简体   繁体   中英

Hide groups based on textbox value in group footer (populated with code) - SSRS

I am trying to hide a group based on whether or not any running total rows have a negative value. I'm building a planning report for manufacturing and want to hide the entire group if there are no shortages for a particular part within the selected timeframe. I'm not sure if I'm even on the right track, but here's what I've tried (SQL 2014):

Add code to get a total:

Dim public totalBalance as Integer
Public Function AddTotal(ByVal balance AS Integer ) AS Integer
                totalBalance = totalBalance + balance    
            return balance
End Function

Public Function GetTotal()
            return totalBalance
End Function

Use the code on each detail row to count the number of rows where the running total is <0.

=Code.AddTotal(SWITCH({FORMULA CALCULATING RUNNING TOTAL}<0,1,TRUE,0))

Add the grand total to the same column but in the group footer:

=Code.GetTotal()

Problem 1 - the GetTotal code appears to be aggregating the entire report every time it prints (up to that point in the report), rather than restarting on the groups. I don't know enough about code in SSRS to tell it to restart counting on a new group. Help there would be appreciated, assuming that I can then successfully hide the non-short groups.

Problem 2 - if I try to hide anything based on that textbox or directly on the Code.GetTotal() expression, it hides everything, even though there are shortages on the report (so I would expect it to either show everything, or show everything after the first shortage).

EDIT:

Here's a simplified screenshot of my actual report output (you can't see the group label, but it's grouped).

Report screenshot

报告截图

Date and Quantity are query fields. Balance is a runningvalue on Quantity. Exception is a SWITCH on the same running value calculation as Balance - there will also be some other exception options here and ultimately I'll want to hide/display based on this field, but I'd like to get it working with just the Quantity first to keep it simple (since it's the runningvalue expression that I think is causing my issues). There is a single group called "Part".

The expression for Balance is

=RunningValue(Fields!NetQty.Value,Sum,"Part")

I need to be able to hide the entire group if all of the Balance values are above zero. If any value is below zero, then I want to see the entire group and all rows in that group, regardless of whether the individual row is below zero or now.

The query is coming from our ERP into a temp table, but it's effectively

SELECT Part, Date, Quantity from PartDetail WHERE Date <= ?UserParameter

There are some other fields, but I don't think they're relevant here (I flip the sign on Quantity to get NetQuantity based on another field if it's an outbound transaction, for example).

I may have misunderstood your requirements but you should be able to do this without any code, using a simple expression.

Assuming you have a group called "Group1" and detail rows contain a column value called "Quantity" that you want to check for negatives..

Right click the group you want to hide in the groups panel at the bottom of the screen, choose properties. In the properties panel click 'Visibility' then set the hidden property to an expression like this. (Assuming you want to hide the group if it does not contain negatives).

=MIN(Fields!Quantity.Value, "Group1") >0 

Note that you are setting the hidden property so if this expression returns True, the group will be hidden. "Group1" sets the scope of the expression to check within each group. By looking for the minimum value, if it's >0 then there must be no negatives present in the group.

Hope that helps.

I hope I've understood you correctly this time !

Here is a sample including a table variable which which I think should be close to the data you are getting from your temp table.

The output should allow you to only require very basic filtering in SSRS or you could extend it as per the comments to only send the filtered data to your report. I left it in in case you might want to have an option to show these too.

First we just create a table that matches the structure of your temp table, you obviously won't need this in the final report but it's handy for testing.

DECLARE @InputData TABLE (Part varchar(10), [Date] Date, Qty int)

INSERT INTO @InputData
VALUES 
('ABC', '2017-01-13', 10),
('ABC', '2017-01-14', 3),
('ABC', '2017-01-15', -4),
('ABC', '2017-01-16', -10),
('ABC', '2017-01-17', 5),
('ABC', '2017-01-18', 6),
('ABC', '2017-01-19', 6),
('DEF', '2017-01-13', 10),
('DEF', '2017-01-14', -5),
('DEF', '2017-01-15', -4),
('DEF', '2017-01-16', 3),
('DEF', '2017-01-17', 2),
('DEF', '2017-01-18', 1),
('DEF', '2017-01-19', -1),
('GHI', '2017-01-13', 12),
('GHI', '2017-01-14', -3),
('GHI', '2017-01-15', -4),
('GHI', '2017-01-16', 4),
('GHI', '2017-01-17', 3),
('GHI', '2017-01-18', 2),
('GHI', '2017-01-19', 1),
('JKL', '2017-01-13', 11),
('JKL', '2017-01-14', 4),
('JKL', '2017-01-15', -6),
('JKL', '2017-01-16', -12),
('JKL', '2017-01-17', 5),
('JKL', '2017-01-18', 6),
('JKL', '2017-01-19', 6)

Now a simple query calculates running totals within Part and then checks if those running totals ever get below zero (again within part)

-- Return data including running total by Part 
-- Starting at the inner query we get the running total within Part
-- The outer query checks the min running total and sets a HideGroup column
SELECT 
    *
    , CASE WHEN MIN(RunTot) OVER(PARTITION BY Part) <0 THEN 1 ELSE 0 END AS HideGroup
    FROM 
    (-- running total within part
    select 
        *
        , SUM(Qty) OVER(PARTITION BY Part ORDER BY [Date]) AS RunTot
         from @InputData
    ) x

-- optionally we could wrap the whole thing in another select and 
-- we could filter out anything where hidegroup =1

The final output looks like this.

Part    Date        Qty RunTot  HideGroup
ABC     2017-01-13  10  10      1
ABC     2017-01-14  3   13      1
ABC     2017-01-15  -4  9       1
ABC     2017-01-16  -10 -1      1
ABC     2017-01-17  5   4       1
ABC     2017-01-18  6   10      1
ABC     2017-01-19  6   16      1
DEF     2017-01-13  10  10      0
DEF     2017-01-14  -5  5       0
DEF     2017-01-15  -4  1       0
DEF     2017-01-16  3   4       0
DEF     2017-01-17  2   6       0
DEF     2017-01-18  1   7       0
DEF     2017-01-19  -1  6       0
GHI     2017-01-13  12  12      0
GHI     2017-01-14  -3  9       0
GHI     2017-01-15  -4  5       0
GHI     2017-01-16  4   9       0
GHI     2017-01-17  3   12      0
GHI     2017-01-18  2   14      0
GHI     2017-01-19  1   15      0
JKL     2017-01-13  11  11      1
JKL     2017-01-14  4   15      1
JKL     2017-01-15  -6  9       1
JKL     2017-01-16  -12 -3      1
JKL     2017-01-17  5   2       1
JKL     2017-01-18  6   8       1
JKL     2017-01-19  6   14      1

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM