I have a table in Power BI that calculates the running total. However, when I add additional columns to the table, the values get messed up. Any ideas on how get the running total to ignore specific columns?
Here is my code:
Measure =
CALCULATE (
SUM ( Append1[AVAILABLE] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <> MAX ( Append1[DUE DATE] )
)
)
+ CALCULATE (
SUM ( Append1[ORDER QTY] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <= MAX ( Append1[DUE DATE] )
)
)
- CALCULATE (
SUM ( Append1[REQUIREMENT QTY] ),
FILTER (
ALL ( Append1[DUE DATE] ),
Append1[DUE DATE] <= MAX ( Append1[DUE DATE] )
)
)
Below are pictures of what the table looks like when it runs correctly and what it looks like when I add another column and the values get messed up.
Correct running total:
Incorrect Running Total:
Thanks in advance for your help!
I was able to get it working correctly using ALLSELECTED. Thanks for your help.
Related
I am facing an issue while calculating differences in the matrix. For example,
In the below image as you can see where there is a blank, the difference is coming 0 whereas it should come 3. I have used the following formula for calculating difference:
Diff =
CALCULATE (
DISTINCTCOUNT ( 'Question and Answer'[Respondent ID] ),
FILTER (
'Question and Answer',
'Question and Answer'[Start Date] = MAX ( 'Question and Answer'[Start Date] )
)
)
- CALCULATE (
DISTINCTCOUNT ( 'Question and Answer'[Respondent ID] ),
FILTER (
'Question and Answer',
'Question and Answer'[Start Date] = MIN ( 'Question and Answer'[Start Date] )
)
)
Any help would be appreciated. Thanks!
I might be mistaken, but it looks like you're calculating the difference between the start and end date of your data, filtered on gender. 'Rather not say' only has data points on one day, therefore it would follow the difference is zero.
What happens is: 3-3 = 0.
What you could do to solve this is to build in logic that evaluates if the min and max start dates are equal, and pass in 0 instead of the value.
Diff =
CALCULATE (
DISTINCTCOUNT ( 'Question and Answer'[Respondent ID] ),
FILTER (
'Question and Answer',
'Question and Answer'[Start Date] = MAX ( 'Question and Answer'[Start Date] )
)
)
- **IF(DATEDIFF(MAX('Question and Answer'[Start Date]),MIN('Question and Answer'[Start Date]),DAY) = 0, 0** ,
CALCULATE (
DISTINCTCOUNT ( 'Question and Answer'[Respondent ID] ),
FILTER (
'Question and Answer',
'Question and Answer'[Start Date] = MIN ( 'Question and Answer'[Start Date] )
)
)
)
The line of code between the two asterix causes the code to work as intended.
I am currently working at updating Tabular model hosted on-prem on a SASS server version 13.0. The tabular model is version 1200.
We have complex logic created in our measures that allow us to define counting rules or to filter depending on parameters selected in disconnected tables.
The model was created using snowflake ‘like’ schema: It can be simplified as below
Link to image: https://ibb.co/WfkmNPV
The error returned by DAX Studio or PowerBI (on an intermittent basis) is the following:
MdxScript(Model) (5174, 18) Calculation error in measure 'Admission'[Number of Clients]: Function 'CONTAINS' does not support comparing values of type Text with values of type Integer. Consider using the VALUE or FORMAT function to convert one of the values.
Sometimes error is resolving by itself without any doing from our end or (sometimes) by reprocessing the model.
In some instances, all our measures are returning this error. In other instances, it’s only a few of them.
The DAX expression for 'Admission'[Number of Clients] is as below:
Admission[Number of Clients] :=
VAR MinDate = MIN ( 'Date'[Full Date] )
VAR MaxDate = MAX ( 'Date'[Full Date] )
VAR AdmissionDate =
FILTER (
Admission,
SWITCH (
TRUE (),
VALUES ( 'Counting Rules'[Counting Rule] ) = "Starts",
Admission[Start Date] >= MinDate && Admission[Start Date] <= MaxDate,
VALUES ( 'Counting Rules'[Counting Rule] ) = "Ends",
Admission[End Date] >= MinDate && Admission[End Date] <= MaxDate,
VALUES ( 'Counting Rules'[Counting Rule] ) = "Active",
Admission[Start Date] <= MaxDate && Admission[End Date] >= MinDate,
0
)
)
RETURN
IF (
COUNTROWS ( VALUES ( 'Counting Rules'[Counting Rule] ) ) = 1,
CALCULATE (
DISTINCTCOUNT ( 'Admission'[ClientKey] ),
FILTER (
AdmissionDate,
IF ( RELATED ( 'Clients'[Date Of Birth] ) <= MAX ( 'Date'[Full Date] )
&& ( ISFILTERED ( 'Client Age'[Age Band] ) || ISFILTERED ( 'Client Age'[Age] ) ),
IF ( COUNTROWS ( VALUES ( 'Age Counting Rule'[Age Counting Rule] ) ) = 1,
INTERSECT (
VALUES ( 'Clients Age'[Age] ),
SELECTCOLUMNS (
ADDCOLUMNS (
VALUES ( Admission[ClientKey] ),
"Age",
ROUNDDOWN (
DATEDIFF (
CALCULATE ( MAX ( 'Clients'[Date Of Birth] ) ),
VAR AdmissionDateFiltered =
IF (
VALUES ( 'Age Counting Rule'[Age Counting Rule] ) = "Age Min",
CALCULATE ( MIN ( Admission[Start Date] ), AdmissionDate, EARLIER ( Admission[ClientKey] ) = Admission[ClientKey] ),
CALCULATE ( MAX ( Admission[End Date] ), AdmissionDate, EARLIER ( Admission[ClientKey] ) = Admission[ClientKey] )
)
RETURN
IF ( VALUES ( 'Age Counting Rule'[Age Counting Rule] ) = "Age Min",
IF ( AdmissionDateFiltered < MIN ( 'Date'[Full Date] ) && NOT ISBLANK ( AdmissionDateFiltered ),
MIN ( 'Date'[Full Date] ),
AdmissionDateFiltered
),
IF ( AdmissionDateFiltered > MAX ( 'Date'[Full Date] ) && NOT ISBLANK ( AdmissionDateFiltered ),
MAX ( 'Date'[Full Date] ),
AdmissionDateFiltered
)
),
DAY
) / 365.25,
0
)
),
"Age", [Age]
)
),
BLANK ()
),
TRUE ()
) //GOM Status
&& IF (
ISFILTERED ( 'Guardianship Status'[Guardianship Type] ) || ISFILTERED ( 'Guardianship Status'[Guardianship Type Description] ) || ISFILTERED ( 'Guardianship Status'[Under Guardianship] ),
CONTAINS (
VALUES ( 'Guardianship Status'[Guardianship Type] ),
'Guardianship Status'[Guardianship Type],
[Child Protection Order Active]
),
TRUE ()
)
)
),
BLANK ()
)
The measure [Child Protection Order Active] in the GOM part of the expression is returning a string value.
The modification I made to the logic is how the Age is calculated.
Have any of you encountered this error?
How would you go by debugging something like that?
Thank you for you time in helping me.
I have resolved my issue by adding a condition around [Child Protection Order Active] to check that it doesn't return blanks.
Measure is always returning a string (at least I assume considering the code) but for some odd reason, the engine think it may return blank (another assumption).
Blank value is considered as integer and therefore returns an error with the above initial DAX expression.
I want the values in between the years mentioned in the image and get their equivalent sales qty.
The sample data is:
Sample Data
For ex: If I select 2005 in the slicer, I should get the qty ordered as 4+5+6 =15 in the new column or as a measure.
Try with this code:
BetweenRange =
CALCULATE (
SUM ( 'Table'[Sales Qty] ),
FILTER (
ALL ( 'Table'[Start Year], 'Table'[End Year] ),
SELECTEDVALUE ( 'YourFilterTable'[Year] ) >= 'Table'[Start Year]
&& SELECTEDVALUE ( 'YourFilterTable'[Year] ) <= 'Table'[End Year]
)
)
I'd suggest using a variable to read in the slicer value:
SumQuantity =
VAR SelectedYear = SELECTEDVALUE ( Slicer[Year] )
RETURN
CALCULATE (
SUM ( Sales[Sales qty] ),
Sales[Start year] <= SelectedYear,
Sales[end Year] >= SelectedYear
)
I am using a measure below to display the months from fact table as described here:
Billings12Months =
CALCULATE (
SUM ( 'Datatable'[Allowable] ),
DATESINPERIOD ( DimDate[Date], MIN ( DimDate[Date] ), +12, MONTH )
)
My attempt to get the running total of above measure is failing:
BillingsRunningTotal =
CALCULATE (
[Billings12Months],
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
BillingsRunningTotal2 =
SUMX (
FILTER (
ALLSELECTED ( DimDate[Date] ),
DimDate[Date] <= MAX ( ( DimDate[Date] ) )
&& YEAR ( DimDate[Date] ) = YEAR ( MIN ( DimDate[Date] ) )
),
[Billings12Months]
)
[BillingsRunningTotal] return same values as [Billings12Months] (please see screen 1 attached) and
[BillingsRunningTotal2] return wrong values and month start from Jan, 17 instead of May, 17 (please see screen-2)
Please help me to calculate the running total. If possible please describe how your solution is working so that I can be better in DAX.
Update:
Please see the screen-3 below for the output when I use the measure suggested by Kosuke:
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
The months are from fact table (not from a Date table) and I think DATESINPERIOD plays a role to calculate and display the months. When we use SUM ( Datatable[Allowable] ), there would be a single month as dictated by the slicer. So we need to use DATESINPERIOD with rolling month calculation logic (DimDate[Date] <= MAX ( DimDate[Date] )) or virtually sum the [Billings12Months], It is where I am failing.
Thanks
You are almost there with the first attempt, however what to calculate is not [Billings12Months], but SUM( Datatable[Allowable] ).
BillingsRunningTotal =
CALCULATE (
SUM ( Datatable[Allowable] ),
FILTER ( ALLSELECTED ( DimDate ), DimDate[Date] <= MAX ( DimDate[Date] ) )
)
Essentially, [Billings12Months] and [BillingsRunningTotal] are same in calculating the sum of Datatable[Allowable], but the only difference is each measure calculates for different scope of period. Therefore, the right way of thinking is to wrap SUM ( Datatable[Allowable] ) in CALCULATE, with different filter parameters.
After going through several posts on StackOverflow and the PowerBI forums, I still can't figure out how to calculate a rolling average based on a given period- in my case, a 30-day rolling average.
Most of the posts I've seen advocate something either identical or really similar to this:
Rolling Sum :=
CALCULATE (
[Sales],
FILTER (
ALL ( Sales ),
[Date]
>= MAX ( Sales[Date] ) - 365
&& [Date] <= MAX ( Sales[Date] )
)
)
(code taken from this post)
...and yet, I can't seem to get the proper values.
In my case, I have the following:
"closing date" for a given loan (column)
loan count (measure)
closing length (column)- length of time (in days) to close a loan
What I'd like to calculate is the rolling 30 day average for any given day. I coded the following:
Rolling Average =
CALCULATE (
SUM(Query1[Closing_Length])/[Loan Count],
FILTER (
ALL ( Query1 ),
[Closing Date].[Date]
>= MAX ( Query1[Closing Date] ) - 30
&& [Closing Date] <= MAX ( Query1[Closing Date] )
)
)
To check the results, I used a visual filter to examine one month's worth of data and these are the results:
Note the totals row at the bottom; for this given period, there are 102 loans and it took an aggregate of 3922 days for them to close. The average I'd like to calculate is 3922/102, which should equal approximately 38.45 days. Instead, we see 42.
How can I fix this?
Measure based solution:
Rolling Average Measure =
VAR A =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Closing Length], 2 )
)
VAR B =
SUMX (
FILTER (
ALL ( 'Query' ),
'Query'[Closing Date] <= MAX ( 'Query'[Closing Date] )
),
ROUND ( 'Query'[Loan Count], 2 )
)
RETURN
A / B
Calculated column based solution:
Rolling Average =
VAR CurrentDate = 'Query'[Closing Date]
VAR T =
FILTER ( 'Query', [Closing Date] <= CurrentDate )
RETURN
ROUND ( SUMX ( T, 'Query'[Closing Length] ) / SUMX ( T, [Loan Count] ), 2 )
Print Screen: