cancel
Showing results for
Did you mean:  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. will give kudos

Thanks

sandeep

1 ACCEPTED SOLUTION  Resident Rockstar

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. 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.
12 REPLIES 12 Anonymous
Not applicable
CALCULATE(SUM('Orders Combined'[LDS]),
FILTER('Calendar',MONTH('Calendar'[Date]) = MONTH(TODAY())-1),
FILTER('Calendar',YEAR('Calendar'[Date]) = IF(MONTH(TODAY())= 1,YEAR(TODAY())-1,YEAR(TODAY()))))

This may be another option Regular Visitor

This works....

• Current Month Value = CALCULATE(SUM(Table1[Value]),MONTH(Table1[Originated])=MONTH(TODAY()))

This does not... Know why is does this?

• previous month = var current_month = MONTH(TODAY())
return CALCULATE(SUM('Table1'[Value]),FILTER('Table1',MONTH('Table1'[Originated])=current_month-1))   Resident Rockstar

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. 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. Regular Visitor

I cannot understand why the LM value does not appear in the yellow cell when I apply the external filter. Can anyone help me? I already appreciate any help please.

I enclose the data file for you to analyze.  Anonymous
Not applicable

Thank you so much, this helped me solve my issue as well!  Helper I

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

@robertsolczak 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.  Helper I

@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. Frequent Visitor

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.  Helper III

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  