Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago

Averagex based on dates

Hi.

 

Can somebody please help me correct this DAX equation? I want to know the average days of inventory based on dates. For visual reasons I have restricted it to 12 months but I want to know the accumulated average days. I am missing something in the DAX because the average days has to be between 400-300 days 🙂

 

Hope it makes sense. 

 

 
VAR _dato = MAX(Kalender[Date])
RETURN
AVERAGEX(
    CALCULATETABLE(
        Data,
        ALL(Kalender[Date]),
        Data[Registreret dato] <_dato && (Data[Afsluttet dato] > _dato || ISBLANK(Data[Afsluttet dato]))
    ),
    DATEDIFF(Data[Registreret dato], _dato,DAY)
)
 
Best regards,
Lisa 

8 Replies

  • I would try to examine the data returned by CALCULATETABLE for a particular date to make sure that it is returning what you expect it to. You can use DAX Studio to run the below query

    DEFINE
        VAR DateFilter =
            TREATAS ( { DATE ( 2022, 6, 30 ) }, 'Kalendar'[Date] )
    
    EVALUATE
    CALCULATETABLE (
        VAR _dato =
            MAX ( Kalender[Date] )
        RETURN
            CALCULATETABLE (
                Data,
                ALL ( Kalender[Date] ),
                Data[Registreret dato] < _dato
                    && (
                        Data[Afsluttet dato] > _dato
                            || ISBLANK ( Data[Afsluttet dato] )
                    )
            ),
        DateFilter
    )
    
    • Anonymous's avatar
      Anonymous
      Not applicable

      johnt75 
      I am not a super user of Power BI, so I dont know the DAX studio. Can I do something else? ğŸ˜¬

      • johnt75's avatar
        johnt75
        Super User

        You could create a temporary table within Power BI

        Temp table =
        VAR DateFilter =
            TREATAS ( { DATE ( 2022, 6, 30 ) }, 'Kalendar'[Date] )
        RETURN
            CALCULATETABLE (
                VAR _dato =
                    MAX ( Kalender[Date] )
                RETURN
                    CALCULATETABLE (
                        Data,
                        ALL ( Kalender[Date] ),
                        Data[Registreret dato] < _dato
                            && (
                                Data[Afsluttet dato] > _dato
                                    || ISBLANK ( Data[Afsluttet dato] )
                            )
                    ),
                DateFilter
            )
        

        You would then be able to examine the table in the data view and see if it contains what you are expecting

  • Anonymous's avatar
    Anonymous
    Not applicable

    johnt75 
    Hi.

    I tested it out and the temp table did show the correct data however where do I go from here? I want to calculate the avarage days.  

    • johnt75's avatar
      johnt75
      Super User

      Your formula looks correct to me, the only thing I can think of is that some other filters are in place. When you restricted the visual to just 12 months did you use the Date table or the Data table? You need to use the Date table because that is what the code is removing filters from.

      Is it possible to share a PBIX with sample data ?

      • Anonymous's avatar
        Anonymous
        Not applicable

        johnt75 

        Yes, I have used the calendar date table for visualization. I dont know if it have something do with Data[Registreret dato] and (Data[Afsluttet dato] -> these dates are from my data table but they are connected to the calendar table ğŸ˜¬