Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hello,
On a given date, I need to retrieve sales for the month corresponding to this date, sales from the previous month, sales from the previous month and so on.
I have written the following dax code:
Calcul =
VAR _LastDate =
CALCULATE(
MAX ( Ventes[Date] ),
ALL (Ventes[Date])
)
VAR _FinMois = EOMONTH ( _LastDate, 0 )
VAR _DebutMois = EOMONTH ( _LastDate, -1 )
VAR _DebutMoisPrec = EOMONTH ( _DebutMois, -1 )
VAR _DebutMoisPrec_2 = EOMONTH ( _DebutMoisPrec, -1 )
VAR _DebutMoisPrec_3 = EOMONTH ( _DebutMoisPrec_2, -1 )
VAR _VentesMois =
CALCULATE (
[Total ventes],
FILTER ( Ventes, Ventes[Date] > _DebutMois && Ventes[Date] <= _FinMois )
)
VAR _VentesMoisPrec =
CALCULATE (
[Total ventes],
FILTER ( Ventes, Ventes[Date] > _DebutMoisPrec && Ventes[Date] <= _DebutMois )
)
VAR _VentesMoisPrec_2 =
CALCULATE (
[Total ventes],
FILTER ( Ventes, Ventes[Date] > _DebutMoisPrec_2 && Ventes[Date] <= _DebutMoisPrec )
)
RETURN
"Dernier mois " & _DebutMois & " " & _FinMois & " " & _VentesMois &
"-Mois Prec " & _DebutMoisPrec & " " & _DebutMois & " " & _VentesMoisPrec &
"-Mois Prec 2 " & _DebutMoisPrec_2 & " " & _DebutMoisPrec & " " & _VentesMoisPrec_2
Here is the result :
The result is correct for the total but not for each customer
My problem is that it works well for the current month (data returned by the _SalesMonth variable) but not for previous months (_SalesMonthPrec and _SalesMonthPrec_2 variables).
I don't understand why this works fine for the current month but not for previous months.
Of course, the results currently being returned are for debugging purposes.
Can you help me?
Solved! Go to Solution.
Hi @Etienne_Bar
I will use this for the last month sales:
lastMonthSales =CALCULATE([Sales], PARALLELPERIOD('Calendar'[Date],-1,MONTH))
It also works with PREVIOUSMONTH but your solution is more elegant
Calcul avec PreviousMonth =
VAR _VentesMois = CALCULATE([Total ventes])
VAR _VentesMois_1 = CALCULATE([Total ventes] , PREVIOUSMONTH(TableDate[Date]) )
VAR _VentesMois_2 = CALCULATE([Total ventes] , PREVIOUSMONTH(PREVIOUSMONTH(TableDate[Date])) )
VAR _VentesMois_3 = CALCULATE([Total ventes] , PREVIOUSMONTH(PREVIOUSMONTH(PREVIOUSMONTH(TableDate[Date]))) )
Thanks a lot !
Hi @Etienne_Bar
I will use this for the last month sales:
lastMonthSales =CALCULATE([Sales], PARALLELPERIOD('Calendar'[Date],-1,MONTH))
User | Count |
---|---|
22 | |
11 | |
8 | |
6 | |
6 |
User | Count |
---|---|
25 | |
12 | |
11 | |
8 | |
6 |