SSRS How to Compare Columns to First Column in Group - sql

I'm trying to create what seems like should be a pretty simple matrix report and I'm hoping someone can help. I have dataset that returns sales region, Date, and sales amount. The requirement is to compare sales for the various time periods to the current date. I'm looking to get my matrix to look something like this:
CurrentSales Date2Sales CurrentVSDate2 Date3Sales CurrentVSDate3
1 1000 1500 -500 800 200
2 1200 1000 200 900 300
3 1500 1100 400 1400 100
I can get the difference from one column to the next, but I need all columns to reference the CurrentSales column. Any help would be greatly appreciated.
Currently my data set is pulling in a date, region, product and sales amount. I then have three parameters, CurrentDate, PreviousMonth, PreviousQuarter. The regions and products are my row groups and the dates are the column groups. Next I added a column inside the group with the following expression: =Sum(Fields!SalesAmount.Value)-Previous(Sum(Fields!SalesAmount.Value),"BookingDate"). I know this isn't correct because it compares the values to the previous date in the column group and I need the comparision to be to the First date in the column group.

Example:
Using Expressions you can:
=iif(Sum(Fields!SalesAmount.Value)= Previous(Sum(Fields!Date2Sales.Value)),
=iif(Sum(Fields!EndBalance.Value)=0, Nothing, Sum(Fields!EndBalance.Value)) You can also use Switch.

The easiest way to get this result would probably be in your query. Add a field to every row returned maybe called "Current Sales." Use a correlated subquery there to get the right value for comparison. Then your comparison can be as simple as =Fields!Sales.Value - Fields!CurrentSales.Value or similar.
There are some ways to do this at the report level, but they are more of a pain: my current favorite of those is to use custom code embedded in the report. Another approach is to use Aggregates of aggregates.

Related

Power pivot ytd calculation

Ok, I have watched many videos and read all sorts and I think I am nearly there, but must be missing something. In the data model I am trying to add the ytd calc to my product_table. I don't have unique dates in the product_table in column a and also they are weekly dates. I have all data for 2018 for each week of this year in set rows of 20, incrementing by one week every 20 rows. E.g. rows 1-20 are 01/01/2018, rows 21-40 are 07/01/2018, and so on.
Whilst I say they are in set rows of 20, this is an example. Some weeks there are more or less than 20 so I can't use the row count function-
Between columns c and h I have a bunch of other categories such as customer age, country etc. so there isn't a unique identifier. Do I need one for this to work? Column i is the sales column with the numbers. What I would like is a new column which gives me a ytd number for each row of data which all has unique criteria between a and h. Week 1 ytd is not going to be any different. For the next 20 rows I want it to add week1 sales to week2 sales, effectively giving me the ytd.
I could sumproduct this easily in the data set but I don't want do that. I want to use dax to save space etc..
I have a date_table which does have unique dates in the main_date column. All my date columns are formatted as date in the data model.
I have tried:
=calculate(products[sales],datesytd(date_table[main_date]))
This simply replicates the numbers in the sales column, not giving me an ytd as required. I also tried
=calculate(sum(products[sales]) ,datesytd(date_table[main_date]))
I don't know if what I am trying to do is possible. All the youtube clips don't seem to have the same issues I am having but I think they have unique dates in their data sets.
Id love to upload the data but its work stuff on a work computer so cant really. Hope I've painted the picture quite clearly.
Resolved, after googling sumif dax, mike honey had a response that i have adapted to get what i need. I needed to add the filter and earlier functions to my equarion and it ended up like this
Calculate (sum(products[sales]),
filter (sales, sales[we_date] <=earlier(sales[we_date]),
filter (sales, sales[year] =earlier(sales[year]),
filter (sales, sales[customer] =earlier(sales[customer]))
There are three other filter sections i had to add, but this now gives me the ytd i needed.
Hope this helps anyone else

Need to perform some not so straight forward data processing in Access 2010

I have a table in Access that is setup like the one in the photo. What I need to do is this:
For each part no, I want to sum the total Qty for each month and type (Ordered and Demand). Then I need to cap the qty in the rows where the type is = to Orders to the value of the Qty where the type is = to Orders, when the sum of the Qty for Ordered is greater than Demand. Let me try to explain it another way.
I want to look at a subset of the master data, in this case the subset is by part no (rows with identical part numbers). For this subset I want to have two sets of sums. 1. The sum of qty with type = Ordered AND 2. a sum of qty with type = Demand. If the sum for Ordered is greater than demand, I want to change the Qty for Ordered to be the value of the Qty for Demand.
Essentially, the business reason is that for reporting purposes the total Qty for Ordered shouldn't be more than Demand in a given month, for a part number.
Looking at the photo, the rows in red will need to change because the sum of the qty is 30, which is greater than the sum of qty for the green rows (25). The red rows qty should be changed to 20 and 5 to match the green rows.
Whew, hope this made sense because it is hard to explain. I have tried many things for a couple weeks now, and I am a bit fuzzy on the details so I will just give a high level. Ok so what have I tried:
I have tried to join the table to iself, using part no (and date I believe) to join on, but that doesn't work because the sum would somehow be incorrect sometimes.
Pivot the table, using the transform and pivot functions in Access but it's important for me to keep the individual dates in tact and when I pivoted it I had to roll it up on a month basis. This gives me the row structure I need to make the changes but I don't know how to get back the original date format after I am done.
I am guessing I need some VBA code that loops through each part no, but I am not big on VBA code and I don't have much time to learn it. Any suggestions? I know this is long winded but its a complicated problem (at least for me). Thanks in advance.

Using SQL Can I get incremental changes in data from query results? Loops?

NOTE: I am not making changes to a database. I am creating a report.
The purpose of the report is to show pending orders that need to be assembled for shipment, but not until there is enough stock to fill the order. An order includes multiple inventory items, but the inventory on hand must be >= the ordered amount per each inventory item and in order by oldest date first before the order can be added to the report.
I've written this to where it pulls the orders, but I need it to loop through to the next order and carry over the quantity of inventory On Hand from the calculation prior to this order. When the calculation is < 0, I don't need to see the order.
EXAMPLE OUTPUT:
Order Date | Order No | Item No | Quantity Ordered | On Hand | Available Qty
2015-01-01 123456 555555 50 60 10
2015-01-02 555544 555555 10 10 00
Notice On Hand says 60 for Item No 555555 in the first row. This is the actual QOH, but the report needs to subtract the amount that was ordered in the previous line from my On Hand stock, and give me the remainder, or show the new available total under On Hand. When my On Hand amount can't fulfill an order, I don't want the order to appear on my report. My current report shows On Hand to be 60 in both rows, and instead of zero, like above, it just subtracts 10 from 60, as if it's my only order.
I don't know what approach to take to do this type of incremental change in a field, but I am assuming it involves a loop and a variable (If I need to add a variable, then it needs to begin with the actual Quantity on hand), ???? Could someone please assist me with a direction? My search to answer this has only left me more unsure of how to do this. I can provide the SQL, but it is rather complicated, so I am trying to keep this on a more general level.
"Looping" should be used as a last resort in SQL. You can do so using a CURSOR but they tend to run slower and require more work than standard SQL commands.
I would recommend trying to break this problem down into smaller tables using sub-queries / CTEs (Common Table Expressions). Can you create a query that shows the total on hand amounts for each item number? Now put that into a sub-query and start building on top of it.

How to calculate the percentage using date fields?

I want to calculate the percentages using date fields in my SSRS report.
I have two date fields :
eg I have 3 columns in my matrix
monthly target,
monthly completed and
percentage
Due date field count 10
completed date count 5
percentage 50%
for example
=Sum(Fields!Completeddate.Value)/Sum(Fields!duedate.Value)*100
however, this will not work for date fields.
Can anyone help?
Thanks in advance
The most straight-forward way to do this would be to add this field to your stored procedure (since it represents a column in your matrix) instead of trying to calculate it in SSRS.
Your SQL statement may look like this:
SELECT #CompletedDate, #DueDate, CAST(DateDiff(day, GetDate(), #CompletedDate) AS FLOAT)/CAST(DateDiff(day, GetDate(), #DueDate) AS FLOAT) * 100 AS INT) AS [Percentage]
The exact implementation will depend on how exactly you're using this value, but the point is that you can use the DateDiff() function to determine how many days apart two dates are, and with that information, you can find a percent difference. Once you've calculated this, you can assign it to a matrix column like you would any other value.

summing up only certain values in column based on name in ssrs reports

By default, the SUM - sums up all the column values. But in my case, i am having a report which is grouped by Name. A name can have single offer with multiple start date's. So, a report has to display each entry for all different start date i.e Same name, offer, players only difference is the date. So for ex, when you sum up the players, only one entry per name needs to taken into account. Because, even though it has multiple start date, other entries are same and duplicated.
The expected result should be like,
The offer cost $10 refers to same $10, so it should be added only once. Similarly for players, etc., But i need the display as shown above, each entries should be shown.
How to solve this?
If all you want to do is avoid aggregating the value in the group total row, as in your example, just remove the aggregation from the expression, i.e. change:
=Sum(Fields!Players.Value)
to:
=Fields!Players.Value
This just returns the first Players value in the Scope - since it's the same value for every row this should be fine.
If you need to further aggregate this value to something like a grand total row, you have a couple of options.
For 2008R2 and above, you can use nested aggregates as an expression in the report - something like:
=Sum(Max(Fields!Players.Value,"MyGroup"))
For 2008 and below, you will need to add the aggregate value to each row in the Dataset and use this without aggregation in the report as required.
I haven’t worked with SSRS much but if this was a regular SQL query you would have to group by date range.
Try adding start date column and check if you can add another group by on top of what you already have.
It would be useful if you can provide more details here like table schema you use for retrieving the data.