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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
TimoMeyer
Frequent Visitor

IF Statement for Months only returns value for one month and doesn´t go into the "else" part

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-1680455605450.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, ...)
1 REPLY 1
MohammadLoran25
Super User
Super User

Hi @TimoMeyer ,

Try This:

 

AvgMeasure =
DIVIDE (
    CALCULATE (
        SUM ( MainSaleTable[Quantity] ),
        FILTER (
            ALL ( DateTable ),
            DateTable[Year] = MAX ( DateTable[Year] )
                && DateTable[Month] <= MAX ( DateTable[Month] )
        )
    ),
    CALCULATE (
        DISTINCTCOUNT ( DateTable[Month] ),
        FILTER (
            ALL ( DateTable ),
            DateTable[Year] = MAX ( DateTable[Year] )
                && DateTable[Month] <= MAX ( DateTable[Month] )
        )
    )
)

 

If this answer solves your problem please accept it as a solution.

Regards,

Loran

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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