How to calculate a measure based on a decreasing/increasing column's value in DAX - dax

I have a Sales table and related dimension tables. MySales table contains columns : Week, StoreID, SalesSeasonID, ProductKey and metrics. My dimensions are related to sales table (Date,SalesSeason,Store,Product tables).
I need to find Sales Quantity (LastYear and LastSeason), as a measure
You can find a sample below:
My purpose is when user selected SaleseasonID[4] then it will return 2 as SalesQuantity.
How can I calculate this measure by DAX formula?

Try:
PYSales =
SUMX (
VALUES ( Table1[YearWeek] ),
CALCULATE (
SUM ( Table1[SalesQuantity] ),
ALL ( Table1[SalesSeasonID] ),
FILTER (
ALL ( Table1[YearWeek] ),
Table1[YearWeek] = EARLIER ( Table1[YearWeek] ) - 100
)
)
)
Worked example PBIX file, using your sample data: https://pwrbi.com/so_55703551/

Related

Retrieving a maximum value from a SUMMARIZECOLUMNS table

I have a query and the following results, executed from DAX Studio:
What I would like to do now is to expand the query so that I can retrieve maximum Total Sales from the table that SUMMARIZECOLUMNS produces. For example, based on the rows displayed in the results, I'd like a way to return 10234.35. Is there a way to do this?
Wrap the whole SUMMARIZECOLUMNS part in a MAXX.
MAXX(
SUMMARIZECOLUMNS([...]),
[Total Sales]
)
The MAXX(<table>,<expression>) function iterates through each row of the <table> from its first argument taking the maximum value of the <expression> in the second argument.
As #greggyb points out, a more efficient implementation would be
CALCULATE (
MAXX ( VALUES ( Customers[Customer Key] ), [Sales Amount] ),
FILTER ( Products, Products[Product Name] = "Fabrikam Laptop12v M2080 Silver" ),
FILTER ( 'Calendar', 'Calendar'[Calendary Year] = 2008 )
)
since this doesn't require creating the whole summary table in memory.

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.

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 | To override active filter with Inactive (Userelationship)

I need a help in creating DAX measures. Here is the relationship in Tabular cube.
I have one Fact sales, Initial Date, final Date and product table.
Relationship : Product -> Fact Sales (1 to Many, active)
Initial Date -> Fact Sales (1 to many, inactive relation)
Final Date - > Fact Sales(1 to many, active)
Cube is used as source for Power Bi tools. Users can select any initial and final date for comparison of sales.
Actual data
Product|Date|Sales
Product1|20160101|100
Product1|20160102|110
Product1|20160131|200
Product2|20160101|78
.....
Expected output
Filters :
product : Product1
Initial date : 20160101
Final Date : 20160131
Product | Initial Sales | Final Sales
Product1|100 |200
I have created DAX measure AS
Initial Sales := CALCULATE(SUM(SALES),USERELATIONSHIP('Fact Sales'[Date],'Initial Date'[Date]))
But this measure is not giving me initial sales as it is also filtered by final date.
Thanks in advance
You can relate tables without an explicit relationship by using FILTER function in DAX.
Since Initial Date relationship with Fact Sales is disabled, you need to FILTER the Fact Sales rows that match the Initial Date selected in the filter. You can use EARLIER to compare the context being evaluated against each row in Fact Sales and filter the right values for the calculation.
Use these measures:
Initial Sales :=
IF (
ISFILTERED ( InitialDate[InitialDate] ),
CALCULATE (
SUM ( FactSales[Sales] ),
FILTER (
ALL ( FactSales ),
COUNTROWS (
FILTER (
FactSales,
EARLIER ( FactSales[Date] ) = MAX ( InitialDate[InitialDate] )
&& EARLIER ( FactSales[Product] ) = [Product]
)
)
)
),
BLANK ()
)
Final Sales :=
IF (
ISFILTERED ( FinalDate[FinalDate] ),
CALCULATE (
SUM ( FactSales[Sales] ),
FILTER ( FactSales, [Date] = MAX ( FinalDate[FinalDate] ) )
),
BLANK ()
)
This expression works as follows, it sums all values in Sales column which Date is equal to the Final Date selected in the filter. However this can be calculated easily as you know with the relationship you have set between Final Date and Fact Sales.
The ISFILTERED functions tell us if the filter have a value selected or not, if a value is selected it returns the value otherwise returns BLANK.
This is an example in Power BI since I don't have access to Excel right now.

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