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.
Related
I would like to calculate column 'E' in DAX
I wanted to count distinct 'B'
based on 'C' and filter on 'D' = instock.
I later want to sum column E -- what is the best way to do this to exclude the duplications
Thanks in advance
You mean you want a Calculated Column?
=
VAR ThisProductCode = 'Table'[product code]
RETURN
CALCULATE(
DISTINCTCOUNT( 'Table'[size] ),
FILTER(
'Table',
'Table'[product code] = ThisProductCode
&& 'Table'[stock] = "instock"
)
)
Not sure what you mean by your second request, though.
Your question is split into 2 parts: Go step by step then:
First Step Code:
Result =
CALCULATE (
DISTINCTCOUNT ( 'Product'[size] ),
ALLEXCEPT ( 'Product', 'Product'[product code] ),
'Product'[stock] = "instock"
)
Second Step:
Result =
VAR TblSummary =
ADDCOLUMNS (
VALUES ( 'Product'[product code] ),
"DISCTotal",
CALCULATE (
DISTINCTCOUNT ( 'Product'[size] ),
ALLEXCEPT ( 'Product', 'Product'[product code] ),
'Product'[stock] = "instock"
)
)
RETURN
SUMX ( TblSummary, [DISCTotal] )
I need to manipulate the Sales measure to exclude any transactions which have been reversed.
Sales measure is as follows right now:
Sales ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE ( VALUES ( 'Exchange Rates'[ExchangeRate] ) )
)
)
What steps and where would I add to extract Reversed Transactions from this measure? I tried below as that's what you'd do in EXCEL but that is not working - when pulling results, it can't display anything on the visual
Sales - Reversals ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE ( VALUES ( 'Exchange Rates'[ExchangeRate] ) ) - SUMX ('Sales', 'Sales [Trans_Type] = "Reversed")
)
)
)
If what you want is to exclude the reversed transactions from your first code, maybe this could help:
Sales ($) =
IF (
HASONEVALUE ( 'Currency'[Detail] ),
CALCULATE(
SUMX (
'Sales',
'Sales'[Value_Sold]
* CALCULATE (
VALUES ( 'Exchange Rates'[ExchangeRate] )
)
),
'Sales'[Trans_Type] <> "Reversed"
)
)
It's basically adding the SUMX into a CALCULATE function which let's you make the some applied to filters (in this case not reversed transactions).
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 am trying to create a measure that will show booked revenue over time, grouped by a few categories. I also want to show as 0 within line charts when the data is BLANK(), so I have it in an IF statement. Everything works just fine except for whatever reason I cannot get it to only show dates prior to today.
Here is the current Measure:
M_BookedRevenue =
VAR CurrentAccountingYear =
CALCULATE ( MIN ( 'Date'[AccountingYear] ), 'Date'[Date] = TODAY () )
VAR CurrentDate =
CALCULATE ( MIN ( 'Date'[SortDate] ), 'Date'[Date] = TODAY() )
RETURN
CALCULATE (
IF (
ISBLANK (
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
),
0,
SUMX (
FILTER (
GROUPBY (
RevenueChannel,
RevenueChannel[ChannelBucket],
RevenueChannel[Channel],
'Date'[Date],
'Date'[SortDate],
'Date'[AccountingWeek],
'Date'[WeekNumber],
'Date'[AccountingMonthEnglishAbbrYear],
'Date'[AccountingMonth],
'Date'[AccountingYear],
"Revenue", SUMX ( CURRENTGROUP (), RevenueChannel[TransactionRevenue] )
),
'Date'[AccountingYear] >= CurrentAccountingYear
&& 'Date'[SortDate] < CurrentDate
),
[Revenue]
)
)
)
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.