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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
Anonymous
Not applicable

IF Statement for Months only returns value for first month

Hello everybody,

I want to create an IF Statement that returns the YTD average sales for each month. The syntax in the Formula is correct but I always only get a value returned for 2023/01. My Goal would be to have a bar for each month that shows the ytd average sales, so for Feb is is the sales of Jan + Feb divided by 2 etc. 

Any idea why I only get a value returned for 2023/01?

 

TimoMeyer_0-1680248360357.png

 

 

 

AVG YTD Sales Actual 2022 = 

IF(
    MIN('IMTT: MASTER TABLE'[Year_Month]) = "2023/01",
    VAR YTD_Sales =
    CALCULATE(SUM('IMTT: MASTER TABLE'[ACT Sales 2022]),
    'IMTT: MASTER TABLE'[Year_Month] = "2023/01")
    VAR YTD_AVG_Sales = DIVIDE(YTD_Sales,1)
    RETURN YTD_AVG_Sales,
   

    IF(
    MIN('IMTT: MASTER TABLE'[Year_Month]) = "2023/02",
    VAR YTD_Sales =
    CALCULATE(SUM('IMTT: MASTER TABLE'[ACT Sales 2022]),
    'IMTT: MASTER TABLE'[Year_Month] = "2023/01",
    'IMTT: MASTER TABLE'[Year_Month] = "2023/02")
    VAR YTD_AVG_Sales = DIVIDE(YTD_Sales,2)
    RETURN YTD_AVG_Sales,

    IF(
    MIN('IMTT: MASTER TABLE'[Year_Month]) = "2023/03",
    VAR YTD_Sales =
    CALCULATE(SUM('IMTT: MASTER TABLE'[ACT Sales 2022]),
    'IMTT: MASTER TABLE'[Year_Month] = "2023/01",
    'IMTT: MASTER TABLE'[Year_Month] = "2023/02",
    'IMTT: MASTER TABLE'[Year_Month] = "2023/03")
    VAR YTD_AVG_Sales = DIVIDE(YTD_Sales,3)
    RETURN YTD_AVG_Sales, ...)
4 REPLIES 4
v-rzhou-msft
Community Support
Community Support

Hi @Anonymous ,

 

I suggest you to create a Calendar table to help you calcualte the average.

Calendar =
ADDCOLUMNS (
    CALENDAR ( DATE ( 2023, 01, 01 ), DATE ( 2023, 12, 31 ) ),
    "Year_Month", FORMAT ( [Date], "YYYY/MM" ),
    "YearMonth Sort",
        YEAR ( [Date] ) * 100
            + MONTH ( [Date] ),
    "Year", YEAR ( [Date] )
)

Data model:

vrzhoumsft_0-1680598914563.png

Measure:

AVG = 
VAR _YTD =
    CALCULATE (
        SUM ( 'IMTT: MASTER TABLE'[ACT Sales 2022] ),
        FILTER (
            ALL ( 'Calendar' ),
            'Calendar'[YearMonth Sort] <= MAX ( 'Calendar'[YearMonth Sort] )
                && 'Calendar'[Year] = MAX ( 'Calendar'[Year] )
        )
    )
VAR _MONTH =
    CALCULATE (
        DISTINCTCOUNT ( 'Calendar'[Year_Month] ),
        FILTER (
            ALL ( 'Calendar' ),
            'Calendar'[YearMonth Sort] <= MAX ( 'Calendar'[YearMonth Sort] )
                && 'Calendar'[Year] = MAX ( 'Calendar'[Year] )
        )
    )
RETURN
    DIVIDE ( _YTD, _MONTH )

Result is as below.

vrzhoumsft_1-1680598954468.png

 

Best Regards,
Rico Zhou

 

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

 

 

Anonymous
Not applicable

First of all, thanks a lot! This really helps.

 

One Problem: I have one more level of detail (Plant) in the table where I also habe the ACT sales 2022 and therefore it is not working. How could I make it work with this additional detail level?

Anonymous
Not applicable

Hi @v-rzhou-msft , are you able to help me make it work with the additional level of Plant?

Anonymous
Not applicable

Hi @v-rzhou-msft , are you able to help me make it work with the additional level of Plant?

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

March2025 Carousel

Fabric Community Update - March 2025

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