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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Likhitha
Helper IV
Helper IV

How to calculate sales value when none of the month is selected and lag value is selected?

Hello All,

I'm having sales column.I want to calculate sales based on lag and month selection.I'm having 4 lags(1,2,3,4).

For example ,lag 1 means if my selected month is december then lag month should be november.

I created DAX for that logic and i got the solution.But the problem is if don't select any month and if selected lag,I want the  Output which is shown in the below image.(Lag selection is 1 and no month selection ,so my sales data has to be from Jan-Nov)

DAX:

Lag sales =
var currentmonth=MONTH(MAX(DateTable[Date]))
var Previousmonth= currentdate-SELECTEDVALUE(NewData[Month Lag Number])
var result=CALCULATE(SUM(NewData[Sales]),FILTER(NewData,MONTH(NewData[Date])=Previousmonth))
return
result

Lag Doubt.png

1 ACCEPTED SOLUTION
v-juanli-msft
Community Support
Community Support

Hi @Likhitha 

Create two tables, "lag table" and "month list", they have no relationship with other tables.

Then create a measure

lag sales =
VAR selected_m =
    SELECTEDVALUE ( 'month list'[month list] )
VAR selected_lag =
    SELECTEDVALUE ( 'lag table'[lag] )
VAR lagsales1 =
    CALCULATE (
        SUM ( 'Table'[sales] ),
        FILTER ( 'Table', MONTH ( [date] ) = selected_m - selected_lag )
    )
VAR lagsales2 =
    CALCULATE (
        SUM ( 'Table'[sales] ),
        FILTER ( 'Table', MONTH ( [date] ) <= ( 12 - selected_lag ) )
    )
RETURN
    IF ( ISFILTERED ( 'month list'[month list] ), lagsales1, lagsales2 )

Capture4.JPGCapture5.JPG

Best Regards
Maggie
Community Support Team _ Maggie Li
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

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @Likhitha 

Create two tables, "lag table" and "month list", they have no relationship with other tables.

Then create a measure

lag sales =
VAR selected_m =
    SELECTEDVALUE ( 'month list'[month list] )
VAR selected_lag =
    SELECTEDVALUE ( 'lag table'[lag] )
VAR lagsales1 =
    CALCULATE (
        SUM ( 'Table'[sales] ),
        FILTER ( 'Table', MONTH ( [date] ) = selected_m - selected_lag )
    )
VAR lagsales2 =
    CALCULATE (
        SUM ( 'Table'[sales] ),
        FILTER ( 'Table', MONTH ( [date] ) <= ( 12 - selected_lag ) )
    )
RETURN
    IF ( ISFILTERED ( 'month list'[month list] ), lagsales1, lagsales2 )

Capture4.JPGCapture5.JPG

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

@v-juanli-msft  Absolutely It's Working perfectly...Thanks a lot....Tank you very very much 🙂

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors