Finding difference between columns in column group - sql-server

I am using report builder to create a report showing a budget for a project. The dataset includes line items for both budget and projected. See below for example rows. I am using a matrix with column group to display budget and projected side by side as well as a row group to show section, category, etc. I need to have a variance column that subtracts projected from budget.
I have scoured the interwebs for solutions but nothing that has worked so far. I feel like there has to be simple solution to this given it is something that could be done in a sql query with zero effort. Most solutions are assuming I have two separate fields, but these are dynamic fields pull out with the column group.
Dataset Row Samples
Type Section Cateogry Phase Task Total
Budget Building Kitchen Pre-Construction Cabinet Hardware $100
Projected Building Kitchen Pre-Construction Cabinet Hardware $220
Report sample
COL GROUP This is the column i want
Budget Projected Variance
+Buidling $100 $220 -$120
+Kitchen
+Pre-Con
EDIT: I tried the below solution without success and have already visited every link provided in the second answer. Maybe there is something I am missing, but I ended up just doing everything in the SQL query and not use Column groups. This is 100% the simplest solution. I am very surprised there is no easy way to reference individual columns in a column group. The below may work for others, but I just could not get them to work for me. Not sure why.

You could add an additional column inside the “Type” group (provided that this is the name of your column group). Set the Column Visibility to hide the column by an expression like
= IsNothing(Previous(Field!Type.Value, “Type”)
Calculate the values for that column as
= Previous(Sum(Fields!Total.Value), “Type”) – Sum(Fields!Total.Value)
That should calculate the difference between the values of the previous type and the current type, and
only show that column for the "Projected" type (when there is a previous type).

On the matrix, you can use the group subtotals to achieve this, you only have to overwrite the SUM operation with an expression that subtract to values. There are many link mentioning how to do that or that can helps you:
How to add calculated column from dynamic columns to a matrix
Adding subtotals to SSRS report tablix
How to write Expression to subtract row Group SubTotals
Reporting in SQL Server – Using calculated Expressions within reports

Related

Keep duplicate data in matrix visual Power BI

I'm facing an issue and hope you'll be able to help me.
I have a matrix with a lot of data and I've noticed that if a line have two same rows, Power BI brings them together immediately and sum the numbers.
Do you think that it's possible to tell Power BI do not summarize any row of the matrix?
Thank you.
Sample Screenshot:
The Power BI Matrix behaves similarly to the Excel Pivot Table, in which the aggregated data in the Values area is filtered by rows and columns: Double filter entries make little sense. However, this can be circumvented with a little workaround.
In PowerQuery select your table and add an index column via Add Column - Index Column
In the Power BI report view add this new index column to the Rows field of your matrix
In the formatting pane disable Word wrap for the Column headers and hide the index column by pulling the column width of the index column with the mouse to zero.
You can play around with the position of the index column to what looks best.
But as can be seen, this removes the aggregation in the Values area too.

SQL Server - Mapping table to identify fields to sum

I have a large database of fields around 400, that need to be summed in various different ways.
Currently I do this in Excel with a look up table to identify which field to sum and then use a sum if formula to then sum those columns. How would I replicate this in SQL?
I've seen some examples where you manually type out each field to calculate but this seems very impractical if you are summing up to 300 fields and if there are any changes to that mapping table, then we would have to redo that.
So far my only solution is to copy and paste all the fields to be calculated in excel, and add the correct summation syntax and paste back in to SQL.
Thanks
Lots of ways of doing this;
create a case statement which acts like your sumif and pass the conditions in the case e.g. CASE WHEN amount >=100 THEN SUM(Amount) END as 'New Column'
Create a function and store all your logic in the function similar to above but easy to maintain should your logic change.
if you post your sumif's with a sample I can help you.

How to Subtract row by row in Power Bi?

In this dataset I am trying to develop a column or a measure based upon the hours column. I am trying to determine the difference between the first and second hour rows, the second and third hour rows, etc. and all the way through the entirety of the data.
Note: there are multiple serial numbers in this table; I just used this serial as an example.
I'm not sure this should be tagged with SQL-Server unless you can change the SQL that sources the data. If so you could pre-calculate this inside SQL Server.
If you can change the Power Query that brings the data into the data model you can add an Index column as the data's coming in and use that.
Please see:
How to Compare the Current Row to the Previous Row Using DAX

Need to insert nested totals for text boxes in SSRS List Tablix cell

This article is SOOOOO close: How to create a report with sections and page breaks using SSRS, at this link: How to create a report with sections and page breaks using SSRS. Using a UNION between the tables we can get the vertical hierarchy we want, using the List Tablix. Using a LEFT JOIN between the tables, we can use a regular tablix to get the totals we want but we get all counties first, then all townships rather than as shown, County with its monthly breakdown followed by its townships with their monthly breakdown.
IF we can insert a total, somehow, some way ... it'll work OR if we can get the List Tablix functionality in a full blown Tablix, that should do it.
Need to get totals on these text boxes:
Here is where I need the totals:
Of course the answer was simple -- another developer and I looked at this and he tried another approach -- adding a row and then adding a total. Something like it was tried before but the total arrived at was for every payment.
What can I say? Overthinking is a disease ... find an intervention group! :)

Sorting in cakephp pagination with SR.NO instead of Database ID

Hi guys i have a problem regarding cakephp pagination and sorting
I have setup a paginated view as flows
SR.NO | name | address| phone|...
SR.NO is a simple counter with-in the foreach loop for displaying data instead of database ID
The below link shows how my SR.NO is created
CakePHP: Numbered Paginated Results
All is fine till now...
Now i have a requirement to sort the result based on SR.NO but have no idea on how to achieve this...
Pour some light into my head thanks...
If I understand correctly, S.NO is just the row-number in the results, and added with PHP.
If the query does not sort the result and S.NO has no relation with the data from the database, you cannot sort on this column, and it would not make any sense to do so.
Explanation
SQL queries without an 'ORDER BY xxx' will return the results in a random order; SQL Server UNION - What is the default ORDER BY Behaviour
Basically, this means that every time you run the query, the results may come back in a different order.
The S.NO you're adding is based on the order in which the results came back from your query, but because this order is random, each time you view the same page, the same record may get another position, thus another S.NO
Options
Specify a column to sort the results on so that the order of the results is consistent each time. You'll be able to reverse the sort-order as well if desired. S.NO can still be used, but purely for presentation
Alternatively, calculate the row position using SQL (Get row position in MYSQL query). However, even in this case row number is based on the position of each row in the results. If no ORDER BY is present, row number is still 'random' and the same row may get a different S.NO / row number each time the query is executed.
The final option is to sort using JavaScript (as others suggested). however, also because of the 'random' order, this can only be done for the records that are visible on the page. If you're using pagination (page x of y), sorting the entire database table will require a new query on the database (which cannot be ordered because of the whole 'random' issue)

Resources