Dax calculated column for holding max value grouped by date - dax

I have a table that holds ten days of values broken out by hour:
date hour sales
11/20/2019 1 10
11/20/2019 2 20
11/20/2019 3 30
...
11/20/2019 23 230
this pattern is repeated for each date until the current date where a row is inserted on the hour with latest sales
I would like to take the last sales amount for each date and divide the prior rows by that value using DAX. I am first trying to create a calculated column to just hold the max sales value, but for some reason I am getting the max value for the table instead of the group:
Estimated[SalesPct] =
var maxSales = MAXX('Estimated'[Exp_Sales])
return
CALCULATE(divide('Estimated'[Exp_Sales], maxSales)

For calculated column following DAX will do:
Estimated[SalesPct] =
VAR maxSales =
CALCULATE (
MAX ( 'Estimated'[Exp_Sales] ),
ALLEXCEPT ( 'Estimated', 'Estimated'[DateColumn] ) //change to your date column
)
RETURN
DIVIDE ( 'Estimated'[Exp_Sales], maxSales )
Thank

Related

Calculate the sum grouped by 2 other columns

Please I really need your help on this, Hello,
I need to create a measure using DAX that has the sum of the qty, I should take the max date of the table that is between two other dates (Date Slicer) based on the column name, For example, if the selected date of the slicer is between 24/6/2020 and 20/3/2021, I need to calculate the total (sum of the qty of each name 'column' where it's date is the max date that is less then the max selected date
So as you can see in the figure the total should be 50+129+3(For that selected date)
Quantity =
VAR _maxdate =
MAX ( 'Date Table'[Date] )
RETURN
SUM ( CALCULATE([Qty], 'Fact Table'[Date] <= _maxdate )
The CALCULATE function in DAX is very powerful. Essentially, you can pass filter criteria into it to create aggregations. In this case, it sounds like you want to sum [Qty] when the date is < the max date.
Since your table visuals aren't automatically updating, you also may not be implementing a date table correctly.

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.

Calculate the difference between the current and the previous row in DAX

In Analysis Services I have a table for Covid Cases, as shown below:
It shows the cumulative cases on a daily basis for 193 different countries. I would like to add a calculated column to calculate the difference between the current row and the previous row, so I can see the daily new cases. Also, as column Country/Region contains 193 different countries, this calculation needs to be somehow grouped for each country. The Date column should also be in the right order.
How should I sort the table and what DAX function should I use to make this work?
Try this
Column =
VAR current =
CALCULATE ( MAX ( tbl[Value] ) )
VAR prev =
MAXX (
FILTER (
tbl,
tbl[Country] = EARLIER ( tbl[Country] )
&& tbl[Date] < EARLIER ( tbl[Date] )
),
tbl[Value]
)
RETURN
current - prev

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.

Resources