Power BI DAX Formula - dax

I have a PBI star schema model. I'm trying to define a measure in the fact table using a numeric field (health index) that resides in one of the dimension tables and should correspond to the maximum date value (in the date dimension table) that is selected in the slicer. The measure is not something that is aggregated. A simple query joining the fact and dimension tables works fine and looks like this
Country
Date
Health Index
China
3/31/22
6
China
2/28/22
4
China
1/31/22
7
I want to be able to create a measure in the fact table called Current Health Index to display the data such that it shows the index value corresponding to the maximum date in the slicer, so if in the slicer the largest date selected is 3/31/22, I would like the measure to have a value of 6:
Country
Date
Health Index
China
3/31/22
6
I tried the following DAX formula but it gives me a very inflated value. I know the joins are correct, so I don't know what the problem is:
Health Index Current =
var _a =
MAXX(
ALLSELECTED('Dim Date Table'[Date])
,'Dim Date Table'[Date]
)
return
CALCULATE(
MAX('Dim Health Indexes Table'[Health Index])
,'Dim Date Table'[Date] = _a
)
Thanks for any assistance

I didn't get a clear picture with you case, but I'll comment your measure and hope this will help.
Health Index Current =
var _a =
MAXX(
ALLSELECTED('Dim Date Table'[Date])
,'Dim Date Table'[Date]
) -- ALLSELECTED() removes an inner context.
-- So, if you have only 1 layer of the context,
-- then ALLSELECTED()=ALL().
-- This can be the reason why you gets "very inflated value".
-- First, I'd check _a value as measure.
return
CALCULATE(
MAX('Dim Health Indexes Table'[Health Index])
,'Dim Date Table'[Date] = _a
) -- This looks correct.
-- You can try 'Dim Health Indexes Table'[Date]= _a
Try 'Dim Date Table' or VALUES(Dim Date Table'[Date]) instead of ALLSELECTED('Dim Date Table'[Date]) and you'll get the max in the context.
Or try VALUES('Dim Health Indexes Table'[Date]) instead of ALLSELECTED('Dim Date Table'[Date])

Hello please try this measure:
VERSION-1
Health Index Current =
VAR _a =
MAXX ( ALLSELECTED ( 'Dim Date Table'[Date] ), 'Dim Date Table'[Date] )
RETURN
CALCULATE (
MAX ( 'Dim Health Indexes Table'[Health Index] ),
'Dim Date Table'[Date] = _a,
'Dim Health Indexes Table'[Country]
= SELECTEDVALUE ( 'Dim Health Indexes Table'[Country], "China" )
)
VERSION-2
Health Index Current =
VAR _a =
MAXX ( ALLSELECTED ( Dim Date Table[Date] ), Dim Date Table[Date] )
RETURN
CALCULATE (
MAX ( Dim Health Indexes Table[Health Index] ),
FILTER (
SUMMARIZE (
Dim Health Indexes Table,
-- First argument of summarize should be your fact table.
Dim Health Indexes Table[Country],
Dim Date Table[Date]
),
Dim Date Table[Date] = _a
&& Dim Health Indexes Table[Country] = SELECTEDVALUE ( Dim Health Indexes Table[Country] )
)
)

Related

How MAX(Calendar[date]) -as a filter in CALCULATE- evaluates to the current context date when calculating running total?

This Article by Alberto Ferrari shows how to compute the running total over a dimension:
Sales RT :=
VAR MaxDate = MAX ( 'Date'[Date] ) -- Saves the last visible date
RETURN
CALCULATE (
[Sales Amount], -- Computes sales amount
'Date'[Date] <= MaxDate, -- Where date is before the last visible date
ALL ( Date ) -- Removes any other filters from Date
)
In this video
he says: "Maxdate contains the last date that is visible in the context"
Where did the context come from when we are using ALL(Date) which cancels all filters and returns the entire Date table?
Shouldn't Max(Date) be always equal to the Maximum date of the entire Date table? not the one corresponding to the row being calculated.
What is meant by [visible context] hear? how is it defined? And/or how is it treated by CALCULATE when ALL is used?
If anyone please could explain to me -in depth- how the [MAX ( 'Date'[Date] )] evaluated to the date corresponding to the row being calculated and not to the Maximum date of the entire Date table
Thanks.

Creating calculated table with MAX and MIN dates in DAX Power BI

I have the following table, imported in Power BI - QOL_Exp (see screenshot example below)
I need to create a calculated table which will filter out values, where Rating = 999 and,
at the same time, will pick only the highest and the lowest Date values from Date column, based on ClientID (see highlighted grey and peach colored areas).
I highlighted in red font - the values that I expect to see in my calculated table
For example, for ClientID = 3052 I will need the records where Date = 11/20/2020 (lowest date for this ClientID) and Date = 5/17/2021 (highest date for this ClientID)
For ClientID = 2666 I will not need the record where Rating = 999 (one of the conditions)
I managed to filter out (to exclude Rating = 999) but struggling with including only Max and MIN date in the new calculated table
This is my DAX:
QOL = CALCULATETABLE(QOL_Exp, QOL_Exp[Rating]<>999)
How should I modify it in order to only leave Max(Date) and Min(Date) records, based on ClientID?
UPD:
Based on the answer given, slightly updated (see below):
QOL =
FILTER (QOL_Exp, QOL_Exp[Rating] <> 999
&&
(( QOL_Exp[Date] = CALCULATE (MIN ( QOL_Exp[Date] ),
ALLEXCEPT(QOL_Exp,QOL_Exp[ClientID])))
|| QOL_Exp[Date] = CALCULATE (MAX ( QOL_Exp[Date] ),
ALLEXCEPT(QOL_Exp, QOL_Exp[ClientID]))))
QOL =
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& (
QOL_Exp[Date]
= CALCULATE (
MIN ( QOL_Exp[Date] ),
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& QOL_Exp[ClientID] = EARLIER ( QOL_Exp[ClientID] )
)
)
|| QOL_Exp[Date]
= CALCULATE (
MAX ( QOL_Exp[Date] ),
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& QOL_Exp[ClientID] = EARLIER ( QOL_Exp[ClientID] )
)
)
)
)

DAX Measure to Calculate Total Yearly Forecast by Snapshot Period

I want to recreate a sumifs dax measure (not calculated column) to Sum Yearly Totals for each snapshot period (less than or equal to selected slicer Month End) by (forecast)month and Productcategory
In Excel this would be accomplished with the following sumifs function =SUMIFS($D$2:$D$97,$A$2:$A$97,B2,$C$2:$C$97,C2)
That is sum amount if reporting period equals value of month in current table row and category equals value of category in current table row
Table fields:
Snapshot Date (range from 31JAN2020 to 31DEC2020 by month end)
Forecast for Month (range from 31JAN2020 to 31DEC2020 by month end)
Sub Category (bikes, coats)
Amount (number actual sales mtd or projected count of sales)
Type (Actual or projection) actual sales amount to date, or projected for each month to end of year
Snapshot Date,Forecast for Month,Sub Category,Amount,Type,Expected Result fo 31MAR2020
1/31/2020,1/31/2020,Bikes,3,Actual,133
1/31/2020,2/29/2020,Bikes,7,Projection,138
1/31/2020,3/31/2020,Bikes,11,Projection,0
1/31/2020,4/30/2020,Bikes,20,Projection,0
1/31/2020,5/31/2020,Bikes,10,Projection,0
1/31/2020,6/30/2020,Bikes,11,Projection,0
1/31/2020,7/31/2020,Bikes,20,Projection,0
1/31/2020,8/31/2020,Bikes,20,Projection,0
1/31/2020,9/30/2020,Bikes,2,Projection,0
1/31/2020,10/31/2020,Bikes,9,Projection,0
1/31/2020,11/30/2020,Bikes,18,Projection,0
1/31/2020,12/31/2020,Bikes,2,Projection,0
1/31/2020,1/31/2020,Coats,1,Actual,111
1/31/2020,2/29/2020,Coats,8,Projection,136
1/31/2020,3/31/2020,Coats,10,Projection,0
1/31/2020,4/30/2020,Coats,17,Projection,0
1/31/2020,5/31/2020,Coats,12,Projection,0
1/31/2020,6/30/2020,Coats,1,Projection,0
1/31/2020,7/31/2020,Coats,11,Projection,0
1/31/2020,8/31/2020,Coats,4,Projection,0
1/31/2020,9/30/2020,Coats,16,Projection,0
1/31/2020,10/31/2020,Coats,10,Projection,0
1/31/2020,11/30/2020,Coats,10,Projection,0
1/31/2020,12/31/2020,Coats,11,Projection,0
2/29/2020,1/31/2020,Bikes,3,Actual,133
2/29/2020,2/29/2020,Bikes,17,Actual,138
2/29/2020,3/31/2020,Bikes,7,Projection,0
2/29/2020,4/30/2020,Bikes,17,Projection,0
2/29/2020,5/31/2020,Bikes,11,Projection,0
2/29/2020,6/30/2020,Bikes,11,Projection,0
2/29/2020,7/31/2020,Bikes,18,Projection,0
2/29/2020,8/31/2020,Bikes,17,Projection,0
2/29/2020,9/30/2020,Bikes,3,Projection,0
2/29/2020,10/31/2020,Bikes,13,Projection,0
2/29/2020,11/30/2020,Bikes,9,Projection,0
2/29/2020,12/31/2020,Bikes,12,Projection,0
2/29/2020,1/31/2020,Coats,15,Actual,111
2/29/2020,2/29/2020,Coats,7,Actual,136
2/29/2020,3/31/2020,Coats,15,Projection,0
2/29/2020,4/30/2020,Coats,11,Projection,0
2/29/2020,5/31/2020,Coats,1,Projection,0
2/29/2020,6/30/2020,Coats,12,Projection,0
2/29/2020,7/31/2020,Coats,9,Projection,0
2/29/2020,8/31/2020,Coats,13,Projection,0
2/29/2020,9/30/2020,Coats,2,Projection,0
2/29/2020,10/31/2020,Coats,16,Projection,0
2/29/2020,11/30/2020,Coats,19,Projection,0
2/29/2020,12/31/2020,Coats,16,Projection,0
Any help would be appreciated
This is a possible measure that emulates the sumifs expression.
It work at the level of the single row and returns BLANK() if more than one row is in the current filter context
Result =
VAR SelectedMonth =
SELECTEDVALUE ( Snapshot[Forecast for Month] )
VAR SelectedSubCategory =
SELECTEDVALUE ( Snapshot[Sub Category] )
RETURN
IF (
NOT ISBLANK ( SelectedMonth )
&& NOT ISBLANK ( SelectedSubCategory ),
CALCULATE (
SUM ( Snapshot[Amount] ),
REMOVEFILTERS ( Snapshot ),
Snapshot[Snapshot Date] = SelectedMonth,
Snapshot[Sub Category] = SelectedSubCategory
) + 0
)

DAX code change from calculated column to a measure

I have a fact table with settlement_date, product_id, service_id, location_id, and ticket_id and srv_adjusted_earning columns.
I have determined the DAX query to generate a calculated column that sums the srv_adjusted_earning column over the date range: settlement date and settlement date - 27 days (i.e. a 4 week window) as:
=CALCULATE(
SUM(factService[SRV_ADJUSTED_EARNING]),
DATESBETWEEN
(
factService[SETTLEMENT_DATE],
DATEADD(factService[SETTLEMENT_DATE], -27, DAY),
factService[SETTLEMENT_DATE]
),
FILTER(factService, factService[PRO_ID] = EARLIER(factService[PRO_ID])),
FILTER(factService, factService[SER_ID] = EARLIER(factService[SER_ID])),
FILTER(factService, factService[LOC_ID_SELLING] =
EARLIER(factService[LOC_ID_SELLING])),
FILTER(factService, factService[TIS_ID] = EARLIER(factService[TIS_ID]))
)
I am trying to convert this DAX calculated column to a measure and I tried the following:
blob:=CALCULATE
(
SUM(factService[SRV_ADJUSTED_EARNING]),
DATESBETWEEN
(
factService[SETTLEMENT_DATE],
DATEADD(factService[SETTLEMENT_DATE], -27, DAY),
factService[SETTLEMENT_DATE]
),
ALLEXCEPT(factService, factService[PRO_ID]),
ALLEXCEPT(factService, factService[SER_ID]),
ALLEXCEPT(factService, factService[LOC_ID_SELLING]),
ALLEXCEPT(factService, factService[TIS_ID])
)
But I get:
Error: Calculation error in measure 'factService'[blob]: A single value for column 'SETTLEMENT_DATE' in table 'factService' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.
Anybody know how I fix this?
As the error mentions, the issue is with factService[SETTLEMENT_DATE]. In the measure, there is no row context so that it knows which date you are talking about, so you need to specify it somehow. I'd suggest using a variable along these lines:
blob :=
VAR SettleDate = MAX ( factService[SETTLEMENT_DATE] )
RETURN
CALCULATE (
SUM ( factService[SRV_ADJUSTED_EARNING] ),
DATESBETWEEN (
factService[SETTLEMENT_DATE],
SettleDate - 27,
SettleDate
),
ALLEXCEPT (
factService,
factService[PRO_ID],
factService[SER_ID],
factService[LOC_ID_SELLING],
factService[TIS_ID]
)
)
Here the variable picks the maximal settlement date in the current filter context. If that's not exactly what you need, adjust the definition accordingly.

DAX How do I calculate Exchange Rate between two tables?

I have a table Sales with fields: Date, Revenue, and CurrencyCode
I have a table ExchangeRate with fields ValidFrom, ValidTo, ExchangeRate, and CurrencyCode.
I need to multiply revenue by the ExchangeRate, when the sales Date falls between ValidFrom and ValidTo and the CurrencyCode from both tables match.
You can create a calculate column in the Sales table to take the Rate from ExchangeRate table then just multiply Rate by the Revenue value in an additional column or a measure.
Use this expression in the Sales calculated column, I named ExchangeRate
ExchangeRate =
CALCULATE (
MAX ( ExchangeRate[ExchangeRate] ),
FILTER (
FILTER ( ExchangeRate, [CurrencyCode] = EARLIER ( Sales[CurrencyCode] ) ),
[ValidFrom] <= EARLIER ( Sales[Date] )
&& [ValidTo] >= EARLIER ( Sales[Date] )
)
)
Now you can create a measure for multiplying the Rate by the Revenune:
RevenueXRateMeasure = SUM(Sales[Revenue])*SUM(Sales[ExchangeRate])
Or if you prefer a column use:
RevenueXRateColumn = [Revenue]*[ExchangeRate]
Let me know if this helps.

Resources