DAX Measure to Calculate Total Yearly Forecast by Snapshot Period - dax

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
)

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.

Dax on Calculated Column and Measure

I have done an experiment on iterable and non-iterable filter for row context and filter context in Power BI.
My understanding is measure has filter context while calculated column has row context
This is the Dax for measure:
UnitPrice (Sum) =
SUM(
'Sales by Store'[unit_price]
)
UnitPrice (Sumx) =
SUMX(
'Sales by Store',
'Sales by Store'[unit_price]
)
UnitPrice (CalSum) =
CALCULATE(
SUM(
'Sales by Store'[unit_price]
)
)
UnitPrice (CalSumx) =
CALCULATE(
SUMX(
'Sales by Store',
'Sales by Store'[unit_price]
)
)
The output in matrix table is display their respective unit price for each product within the same product category (Whole bean/teas):
This means measure has filter context but why we Sum and Calculate Sum does not display the same amount of total unit price of the product group for each product since both Dax function are not iterable?
For calculated column, I am using another column called Quantity Sold in Power Query.
Qty Sold_1 (Sum) =
SUM(
'Sales by Store',
'Sales by Store'[quantity_sold]
)
Qty Sold_2 (Sumx) =
SUMX(
'Sales by Store',
'Sales by Store'[quantity_sold]
)
Qty Sold_3 (CalSum) =
CALCULATE(
SUM(
'Sales by Store'[quantity_sold]
)
)
Qty Sold_4 (CalSumx) =
CALCULATE(
SUMX(
'Sales by Store',
'Sales by Store'[quantity_sold]
)
)
The output is Sum and Sumx will show the total amount for all the row while Calculate Sum and Calculate Sumx will show their respective unit price.
quantity sold is the original column.
Transaction ID
quantity sold
Qty Sold_1 (Sum)
Qty Sold_2 (Sumx)
Qty Sold_3 (CalSum)
Qty Sold_4 (CalSumX)
131
1
6
6
1
1
192
2
6
6
1
1
460
3
6
6
1
1
Why Sumx (able to iterate by row) is not showing the respective quantity sold in row context and Calculate Sum is not showing the total quantity sold for all the row since it is not iterable by row in row context?
When you write SUM ( Table[Column] ) this is exactly the same as writing SUMX ( Table, Table[Column] ) so when using a SUMX with only one column, these are the same. The flexibility of SUMX is that you can write e.g. SUMX ( Table, Table[Column] * Table[Column2] ) to get something other than just a straight sum, calculated row-by-row in your table.
When you are adding a new calculated column, you need to invoke CALCULATE to transform your table row context into a corresponding filter context. This is called context transition and is very important to understand when writing DAX. Without this context transition, SUM ( Table[Column] ) (and the SUMX equivalent) will give you... the sum of the column. As expected. This has nothing to do with row-by-row iteration.

Dax calculated column for holding max value grouped by date

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

Filter Dax Calculation based upon YTD Sum

In a Tabular SSAS Model, I'm trying to count the number of distinct customers that purchased a given product wtihin a YTD Timeframe. The table contains measures that aren't explicit sums, so I get the Cartesian Product of all products for each customer, regardless of no sales. I'm attempting to limit the count by filtering out customer / product combinations with YTD Sales = 0. However, I cannot get the FILTER to recognize the DATESYTD context. It only ever filters based upon Sales existing within the chosen calendar month. I've tried inserting the ALL function every which way.
This is what I have so far.
Measure:
CALCULATE (
DISTINCTCOUNT ( Fact[Customer] ),
DATESYTD ( Calendar[Date] ),
FILTER ( Fact,
CALCULATE ( [Sum of Sales], DATESYTD ( Calendar[Date] ) ) <> 0
)
)
This measure will, for example, count distinct customers purchasing a product in Month #5 if Month #5 is explicitly chosen. It will not, however, include a customer that purchased that item in Month #2 of the same year.
I think the following DAX should do the trick:
COUNTROWS(
FILTER(
VALUES(Fact[Customer]),
CALCULATE ( [Sum of Sales], DATESYTD ( Calendar[Date] ) ) <> 0
)
)
Also, make sure your 'Calendar' table has been marked as a date table. If, for some reason, you prefer not to mark it as a date table, rewrite the above DAX to:
COUNTROWS(
FILTER(
VALUES(Fact[Customer]),
CALCULATE ( [Sum of Sales], DATESYTD ( Calendar[Date] ), ALL('Calendar') ) <> 0
)
)
Edit: Do you have records in your fact table where [Sum of Sales] is 0? If not, then you could simplify and improve the performance considerably by writing:
CALCULATE(
DISTINCTCOUNT(Fact[Customer]),
DATESYTD( Calendar[Date] )
)
Again, if you haven't marked your 'Calendar' table as a date table, add ALL(Calendar) to remove the filter on specific calendar columns.

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