Power BI is turning 10, and we’re marking the occasion with a special community challenge. Use your creativity to tell a story, uncover trends, or highlight something unexpected.
Get startedJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
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))
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
Check out the June 2025 Power BI update to learn about new features.
User | Count |
---|---|
14 | |
11 | |
10 | |
10 | |
10 |
User | Count |
---|---|
16 | |
13 | |
12 | |
11 | |
8 |