Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
avulasandeep
Helper III
Helper III

Current Month , Previous Month and Before Previous Month

HI  PBI Experts ,

Here is my question for you in dax ,

 

i want to know the sales of the "current month", "last month", "last before month sales"  by using the Dax. 

i am using previousmonth dax function  and i want to know for  current month  , before previous month dax function.


 

currnt month prv month .jpg 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

will give kudos

Advance Thanks,

Thanks 

sandeep

 

1 ACCEPTED SOLUTION
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @avulasandeep,

 

If you only want to calculate the sales of "current month", "last month", "last before month sales"  by using the Dax, you could try the formula below.

 

current_month_sales = CALCULATE(SUM('Table1'[Sales]),FILTER('Table1',MONTH('Table1'[Month])=MONTH(TODAY())))

last_month_sales = var current_month= MONTH(TODAY()) return  CALCULATE(SUM('Table1'[Sales]),FILTER('Table1',MONTH('Table1'[Month])=current_month -1))

last before_month_sales = var current_month= MONTH(TODAY()) return  CALCULATE(SUM('Table1'[Sales]),FILTER('Table1',MONTH('Table1'[Month])=current_month -2))

Here is the output.

 

Capture.PNG

 

Best Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

27 REPLIES 27
Anonymous
Not applicable

Hello, I just wonder, why do you use the 'FILTER' function within 'CALCULATE' - does it have any purpose?

 

I'm asking because for me the formula works without it:

 

Volume 2 Months Before:=CALCULATE(COUNTROWS(Table),MONTH(Table[Date])=MONTH(TODAY())-2)
Anonymous
Not applicable

@Anonymous there are some filter scenarioes where FILTER is 100% required.  From discussions i've had with other datanauts, there is an argument to be made to use the FILTER inside the calculate for optimisation as the filter is used behind the scenes regardless.

 

I still personally avoid filter unless absolutely necessary as it feel it makes for shorter cleaner code, however I am concious when i'm optmising my model, that that is a place for me to review.

 

Anonymous
Not applicable

@Anonymous thanks for such a quick response! I'm also a fan of having the code leaner, but I'm now curious of the cases where you need FILTER - could you send me link to such discussion you mentioned?

Anonymous
Not applicable

Off the top of my head, 2 examples would be if you have complex AND and OR statements.  Also if you do a calculatetable and place it into a variable.

Hi Power BI community,

 

Just a question regarding a small alteration to the question hear.

 

What should be typed in DAX if instead of basing the data around "current month" we want to use the month selected on a slicer as the benchmark?

 

Thanks,

Gdexter

Anonymous
Not applicable

Rather than using DAX for this solution, i'd suggest instead creating a Date Table and store a True/False value against the dates that conform to the periods you care about.  You would end up with 3 different flags.  Then you can create 3 visuals based on the "Sum" of sales, and apply those flags to constrain the dates to the 3 different date ranges.

 

In Power Query you can get todays date by using:

Date.From(DateTime.LocalNow())

You can extract years, months and days from this.  An example of a previous month True/False would be:

 

if [Year] = Date.Year(DateTime.LocalNow()) then if Date.Month(DateTime.LocalNow()) = 1 then [Month Number] = 12 else [Month Number] = (Date.Year(DateTime.LocalNow()) - 1) else "FALSE"

In the code above, this is a column added to my date table that has a YEAR and MONTH column.

i need all the below selected months in dax . 

 

That means
Current month sales using dax :

previous month sales using dax :

before previous month  sales using dax :

 

Thanks 

sandeep

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors