SOLVED

Finding the last offered price

%3CLINGO-SUB%20id%3D%22lingo-sub-3287441%22%20slang%3D%22en-US%22%3EFinding%20the%20last%20offered%20price%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3287441%22%20slang%3D%22en-US%22%3E%3CP%3EAs%20you%20can%20see%20in%20the%20attached%20file.%20We%20have%20the%20current%20price%20and%20various%20offers.%20I%20want%20to%20calculate%20the%20following%3A%3C%2FP%3E%3CP%3E-%20The%20last%20price%20offered.%3C%2FP%3E%3CP%3E-%20The%20final%20price.%20This%20is%20the%20last%20price%20offered%20or%2C%20if%20this%20is%20missing%2C%20the%20current%20price.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20my%20measures%20'Latest%20Offer'%20and%20'Final%20Price'%20I%20can%20calculate%20the%20desired%20values.%3C%2FP%3E%3CP%3EThe%20problem%20is%20the%20Grand%20Total%2C%20which%20shows%20an%20incorrect%20value%20for%20the%20'Final%20Price'.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Roli_Bird_0-1650106864234.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F364760iCDF3873BE41E59BB%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22Roli_Bird_0-1650106864234.png%22%20alt%3D%22Roli_Bird_0-1650106864234.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3287441%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3287535%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20the%20last%20offered%20price%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3287535%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1337252%22%20target%3D%22_blank%22%3E%40Roli_Bird%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYes%2C%20you%20are%20right%2C%20HASONVALUE()%20gives%20more%20correct%20result.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3287533%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20the%20last%20offered%20price%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3287533%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat's%20a%20very%20good%20solution.%20I%20like%20how%20you%20structure%20your%20code.%20But%20when%20we%20filter%20the%20data%2C%20the%20result%20is%20no%20longer%20correct.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22LatestPrice%20B.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F364784iE4CECE897803DEB5%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22LatestPrice%20B.png%22%20alt%3D%22LatestPrice%20B.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EI%20modified%20your%20code%20as%20follows.%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3ELatest%20Offer%20B%3A%3DVAR%20maxDate%20%3D%0A%20%20%20%20MAX%20(%20tblOffers%5BOfferDate%5D%20)%0ARETURN%0A%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20HASONEVALUE(%20tblItems%5BItemCode%5D%20)%3B%0A%20%20%20%20%20%20%20%20CALCULATE%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20VALUES%20(%20tblOffers%5BOfferedPrice%5D%20)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20tblOffers%5BOfferDate%5D%20%3D%20maxDate%0A%20%20%20%20%20%20%20%20)%3B%0A%20%20%20%20%20%20%20%20BLANK%20()%0A%20%20%20%20)%0A%0A%0AFinal%20Price%20B%3A%3DVAR%20price%20%3D%0A%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%5BActual%20Price%5D%3B%0A%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%0A%20%20%20%20)%0ARETURN%0A%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20HASONEVALUE(%20tblItems%5BItemCode%5D%20)%3B%0A%20%20%20%20%20%20%20%20price%3B%0A%20%20%20%20%20%20%20%20SUMX%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20tblItems%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%20%3D%200%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5BActual%20Price%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3ENow%20the%20result%20looks%20better.%20See%20also%20the%20attached%20file.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20do%20not%20get%20me%20wrong.%20Your%20solution%20is%20great.%20I%20would%20never%20have%20come%20up%20with%20your%20solution%20and%20only%20made%20a%20small%20change%20to%20YOUR%20measure.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20for%20your%20support%20and%20solution.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3287503%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20the%20last%20offered%20price%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3287503%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1337252%22%20target%3D%22_blank%22%3E%40Roli_Bird%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20do%20not%20show%20Grand%20Total%20for%20Latest%20Price%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22image.png%22%20style%3D%22width%3A%20480px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F364779i04E5A15E47EB7FAB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22image.png%22%20alt%3D%22image.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3Eit%20could%20be%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3ELatest%20Offer%20A%3A%3D%0AVAR%20maxDate%20%3D%0A%20%20%20%20MAX%20(%20tblOffers%5BOfferDate%5D%20)%0ARETURN%0A%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20ISFILTERED%20(%20tblItems%5BItemCode%5D%20)%2C%0A%20%20%20%20%20%20%20%20CALCULATE%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20VALUES%20(%20tblOffers%5BOfferedPrice%5D%20)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20tblOffers%5BOfferDate%5D%20%3D%20maxDate%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20BLANK%20()%0A%20%20%20%20)%0A%0A%0AFinal%20Price%20A%3A%3D%0AVAR%20price%20%3D%0A%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%20%3D%200%2C%0A%20%20%20%20%20%20%20%20%5BActual%20Price%5D%2C%0A%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%0A%20%20%20%20)%0ARETURN%0A%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20ISFILTERED%20(%20tblItems%5BItemCode%5D%20)%2C%0A%20%20%20%20%20%20%20%20price%2C%0A%20%20%20%20%20%20%20%20SUMX%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20tblItems%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20IF%20(%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%20%3D%200%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5BActual%20Price%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%5BLatest%20Offer%20A%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3287501%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20the%20last%20offered%20price%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3287501%22%20slang%3D%22en-US%22%3EThank%20you%20very%20much.%20291%20is%20what%20I%20expect%20to%20see%20-%20and%20that's%20what%20your%20measure%20calculates.%3CBR%20%2F%3E%3CBR%20%2F%3EMy%20next%20problem%20is%20just%20understanding%20your%20measure.%20You%20use%20CALCULATE%2C%20without%20filter%20and%20inside%20CALCULATE%20you%20use%20SUMX%20in%20table%20tblPrices%20with%20my%20original%20formula%20as%20expression.%3CBR%20%2F%3E%3CBR%20%2F%3ECould%20you%20please%20explain%20in%20two%20or%20three%20sentences%20how%20you%20came%20up%20with%20this%20measure%20or%20how%20the%20measure%20works.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3287468%22%20slang%3D%22en-US%22%3ERe%3A%20Finding%20the%20last%20offered%20price%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3287468%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1337252%22%20target%3D%22_blank%22%3E%40Roli_Bird%3C%2FA%3E%26nbsp%3BWhat%20is%20it%20that%20you%20expect%20to%20see%20in%20the%20yellow%20shaded%20totals%3F%20The%20following%20measure%20will%20sum%20the%20final%20Prices%20column%2C%20resulting%20in%20291.%3C%2FP%3E%3CP%3E%3DCALCULATE(sumx(tblPrices%2CIF(%5BLatest%20Offer%5D%26lt%3B%26gt%3BBLANK()%2C%5BLatest%20Offer%5D%2C%5BActual%20Price%5D)))%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

As you can see in the attached file. We have the current price and various offers. I want to calculate the following:

- The last price offered.

- The final price. This is the last price offered or, if this is missing, the current price.

 

With my measures 'Latest Offer' and 'Final Price' I can calculate the desired values.

The problem is the Grand Total, which shows an incorrect value for the 'Final Price'.

Roli_Bird_0-1650106864234.png

 

5 Replies
best response confirmed by Roli_Bird (Occasional Contributor)
Solution

@Roli_Bird What is it that you expect to see in the yellow shaded totals? The following measure will sum the final Prices column, resulting in 291.

=CALCULATE(sumx(tblPrices,IF([Latest Offer]<>BLANK(),[Latest Offer],[Actual Price])))

Thank you very much. 291 is what I expect to see - and that's what your measure calculates.

My next problem is just understanding your measure. You use CALCULATE, without filter and inside CALCULATE you use SUMX in table tblPrices with my original formula as expression.

Could you please explain in two or three sentences how you came up with this measure or how the measure works.

@Roli_Bird 

If do not show Grand Total for Latest Price

image.png

it could be

Latest Offer A:=
VAR maxDate =
    MAX ( tblOffers[OfferDate] )
RETURN
    IF (
        ISFILTERED ( tblItems[ItemCode] ),
        CALCULATE (
            VALUES ( tblOffers[OfferedPrice] ),
            tblOffers[OfferDate] = maxDate
        ),
        BLANK ()
    )


Final Price A:=
VAR price =
    IF (
        [Latest Offer A] = 0,
        [Actual Price],
        [Latest Offer A]
    )
RETURN
    IF (
        ISFILTERED ( tblItems[ItemCode] ),
        price,
        SUMX (
            tblItems,
            IF (
                [Latest Offer A] = 0,
                [Actual Price],
                [Latest Offer A]
            )
        )
    )

@Sergei Baklan 

That's a very good solution. I like how you structure your code. But when we filter the data, the result is no longer correct.

LatestPrice B.png

I modified your code as follows.

Latest Offer B:=VAR maxDate =
    MAX ( tblOffers[OfferDate] )
RETURN
    IF (
        HASONEVALUE( tblItems[ItemCode] );
        CALCULATE (
            VALUES ( tblOffers[OfferedPrice] );
            tblOffers[OfferDate] = maxDate
        );
        BLANK ()
    )


Final Price B:=VAR price =
    IF (
        [Latest Offer A] = 0;
        [Actual Price];
        [Latest Offer A]
    )
RETURN
    IF (
        HASONEVALUE( tblItems[ItemCode] );
        price;
        SUMX (
            tblItems;
            IF (
                [Latest Offer A] = 0;
                [Actual Price];
                [Latest Offer A]
            )
        )
    )

Now the result looks better. See also the attached file.

 

Please do not get me wrong. Your solution is great. I would never have come up with your solution and only made a small change to YOUR measure.

 

Thank you for your support and solution.

@Roli_Bird 

Yes, you are right, HASONVALUE() gives more correct result.