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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
RegionH
Helper I
Helper I

Calculate if order is too late or on time based on parameter

Hi

 

My datamodel has two tables called 'Indkøbsordrer' and 'Varemodtagelser'. The two tables are connected on [Ordre nøgle] in a one to many relationship.

 

I'm trying to calculate if an order is "Too late" or "On time" based on the following DAX-formula.

 

Ordrer Punktlighed = 
if(
    ISEMPTY(
        Filter(
            CALCULATETABLE(
            'Varemodtagelser', ALLEXCEPT('Varemodtagelser','Varemodtagelser'[Indkøbsordrenummer])),
            sum(Varemodtagelser[Modtagelsesdato])-'Leveringstidsjustering'[Leveringstidsjustering Value]-sum(Varemodtagelser[Statistikdato]) > 0
        )
    ),
    "On time",
    "Late"
)

 

The formula is working fine. But now I would like to distinguish between the value of the column 'Indkøbsordrer'[Indkøbsbilagsart].  If the value in the row is equal to "ZLM" or "ZCD, then use the parameter as is. But if the value is diffent, then use the value 5 days.

 

So I wrote this formula, but it is not working, because when I use this measure in another measure to calculate the sum of "Delayed orders" I get 2.484. The same result as the original formula.

 

Z_Ordrer_Punktlighed_Bilagsart = 
IF(
    ISEMPTY(
        FILTER(
            CALCULATETABLE(
                'Varemodtagelser',
                ALLEXCEPT('Varemodtagelser', 'Varemodtagelser'[Indkøbsordrenummer])
            ),
            SUM('Varemodtagelser'[Modtagelsesdato]) - 
            IF(
                RELATED('Indkøbsordrer'[Indkøbsbilagsart]) = "ZLM" || RELATED('Indkøbsordrer'[Indkøbsbilagsart]) = "ZCD",
                'Leveringstidsjustering'[Leveringstidsjustering Value],
                5
            ) - 
            SUM('Varemodtagelser'[Statistikdato]) > 0
        )
    ),
    "On time",
    "Late"
)

 

I hope someone will take a look at the formula and maybe help me correct it?

 

RegionH_0-1710771259102.png

 

Thanks in advance.

 

Best regards

Morten  

1 ACCEPTED SOLUTION

Thank you for answering my question. The reason for using related is that the table 'Indkøbsordrer' has the information about [Indkøbsbilagsart]. This information is not represented in the table 'Varemodtagelser'. 

 

I wrote another DAX-formula which solved my problem. 

Z_ForsinkedeBilagsarter = 
VAR Forsinkelser_ZLM_ZCD =
    CALCULATE (
        DISTINCTCOUNT ( 'Indkøbsordrer'[Indkøbsordrenummer] ),
        FILTER (
            'Varemodtagelser',
            (
                'Varemodtagelser'[Modtagelsesdato]
                - 'Leveringstidsjustering'[Leveringstidsjustering Value]
                - 'Varemodtagelser'[Statistikdato]
            ) > 0
                && (
                    RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZLM"
                    || RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZCD"
                )
        )
    )
VAR Forsinkelser_Andre =
    CALCULATE (
        DISTINCTCOUNT ( 'Indkøbsordrer'[Indkøbsordrenummer] ),
        FILTER (
            'Varemodtagelser',
            (
                'Varemodtagelser'[Modtagelsesdato]
                - 5
                - 'Varemodtagelser'[Statistikdato]
            ) > 0
                && NOT (
                    RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZLM"
                    || RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZCD"
                )
        )
    )
RETURN
    Forsinkelser_ZLM_ZCD + Forsinkelser_Andre

View solution in original post

2 REPLIES 2
lbendlin
Super User
Super User

what's your reasoning for using RELATED in a measure?

 

Z_Ordrer_Punktlighed_Bilagsart = 
IF(
    ISEMPTY(
        FILTER(
            CALCULATETABLE(
                'Varemodtagelser',
                ALLEXCEPT('Varemodtagelser', 'Varemodtagelser'[Indkøbsordrenummer])
            ),
            SUM('Varemodtagelser'[Modtagelsesdato]) - 
            IF('Indkøbsordrer'[Indkøbsbilagsart]) IN { "ZLM" ,"ZCD" },
                'Leveringstidsjustering'[Leveringstidsjustering Value],5
            ) - 
            SUM('Varemodtagelser'[Statistikdato]) > 0
        )
    ),
    "On time",
    "Late"
)

 

You could also use Countrows instead of IF(ISEMPTY).

Thank you for answering my question. The reason for using related is that the table 'Indkøbsordrer' has the information about [Indkøbsbilagsart]. This information is not represented in the table 'Varemodtagelser'. 

 

I wrote another DAX-formula which solved my problem. 

Z_ForsinkedeBilagsarter = 
VAR Forsinkelser_ZLM_ZCD =
    CALCULATE (
        DISTINCTCOUNT ( 'Indkøbsordrer'[Indkøbsordrenummer] ),
        FILTER (
            'Varemodtagelser',
            (
                'Varemodtagelser'[Modtagelsesdato]
                - 'Leveringstidsjustering'[Leveringstidsjustering Value]
                - 'Varemodtagelser'[Statistikdato]
            ) > 0
                && (
                    RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZLM"
                    || RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZCD"
                )
        )
    )
VAR Forsinkelser_Andre =
    CALCULATE (
        DISTINCTCOUNT ( 'Indkøbsordrer'[Indkøbsordrenummer] ),
        FILTER (
            'Varemodtagelser',
            (
                'Varemodtagelser'[Modtagelsesdato]
                - 5
                - 'Varemodtagelser'[Statistikdato]
            ) > 0
                && NOT (
                    RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZLM"
                    || RELATED ( 'Indkøbsordrer'[Indkøbsbilagsart] ) = "ZCD"
                )
        )
    )
RETURN
    Forsinkelser_ZLM_ZCD + Forsinkelser_Andre

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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