cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
bitbit
Helper I
Helper I

DAX variable how to

I have the following datasets:

1) Calendar

 

2) Actual: captures actual monthly sales

MonthValue
Jan1000
Feb1200
Mar800
etc 

 

3) Forecast: captures forecast sales for remaining months.  Forecasts updated monthly.  eg. In Jan, 12 months' of forecast.  In Feb, 11 months' of forecast....etc

ActMthForecastMthValue
FebMar500
FebApr600
FebMay550
FebJun700
FebJul750
FebAug500
FebSep500
FebOct500
FebNov560
FebDec580
MarApr700
MarMay800
MarJun1000
MarJul1100
MarAug1200
MarSep800
MarOct900
MarNov950
MarDec1300

 

Relationship with Calendar set for #2-3.

 

I have a slicer in my excel worksheet to select "month" from Calendar.

 

I would like to achieve the following:

1) When I select "month" on the slicer, result should show me actual sales and forecast for remaining months accordingly.

eg. if selected month = Mar, report to show actual sales for Jan-Mar, forecast for Apr-Dec

if selected month = Feb, report to show actual sales for Jan-Feb, forecast for Mar-Dec

 JanFebMarAprMayJunJulAugSepOct NovDecTotal Year
Sales             

 

Calculated measures created:

MTD Actual:=TOTALMTD(sum(Actual[Value]),'Calendar'[Date])

MTD Forecast:=calculate(TOTALMTD(sum(Forecast[Value]),'Calendar'[Date]),Forecast[ActMth]=3)

YTD Forecast:=calculate([YTD Actual],'Actual'[Date (Month Index)]<=3)+[MTD Forecast]

 

I have to manually change the month in the formula of "MTD forecast" and "YTD forecast".  Pls advise how to set these 2 measures variable according to slicer selected month.

 

3 REPLIES 3
v-yanjiang-msft
Community Support
Community Support

Hi @bitbit ,

According to your description, here's my solution.

1. Create a Month table. Sort Month column by MonthNo column.

vyanjiangmsft_0-1686045662952.png

2.Create a measure.

Sales =
IF (
    MAX ( 'Month'[MonthNo] ) <= SELECTEDVALUE ( 'Date'[Month] ),
    MAXX (
        FILTER ( Actual, MONTH ( 'Actual'[Date] ) = MAX ( 'Month'[MonthNo] ) ),
        'Actual'[YTD]
    ),
    IF (
        MAX ( 'Month'[Month] ) <> "Total Year",
        MAXX (
            FILTER (
                'Forecast',
                'Forecast'[ActMth] = SELECTEDVALUE ( 'Date'[Month] )
                    && MONTH ( 'Forecast'[Date] ) = MAX ( 'Month'[MonthNo] )
            ),
            'Forecast'[Value]
        ),
        SUMX (
            FILTER ( Actual, MONTH ( 'Actual'[Date] ) <= SELECTEDVALUE ( 'Date'[Month] ) ),
            'Actual'[YTD]
        )
            + SUMX (
                FILTER (
                    'Forecast',
                    'Forecast'[ActMth] = SELECTEDVALUE ( 'Date'[Month] )
                        && MONTH ( 'Forecast'[Date] ) > SELECTEDVALUE ( 'Date'[Month] )
                ),
                'Forecast'[Value]
            )
    )
)

Put Month column from Month table in Matrix column and the measure in values, get the result:

vyanjiangmsft_1-1686045766513.png

I attach my sample below for your reference.

 

Best regards,

Community Support Team_yanjiang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

v-yanjiang-msft
Community Support
Community Support

Hi @bitbit ,

I'm not quite clear about the problem, as your description "Relationship with Calendar set for #2-3", do you mean the Calendar table have relationship with Actual table and also Calendar table have relationship with Forecast table? If so, which column are they connected, there's not date column in Actual and Forecast tables. Besides, I create a sample and attach it below, could you please reproduce your problem in my sample and send back.

 

Best regards,

Community Support Team_yanjiang

Pls advise solution to technical issue included in the excel file, link below:

https://1drv.ms/x/s!Aj1jIHqeNbLXlTreQvuRmDdgXqAC?e=CFbmeh 

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Top Solution Authors
Top Kudoed Authors