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
capko
Helper II
Helper II

Complex problem with several criteria using PBI / DAX

Hello,

 

I've an interesting problem to solve but I need some in order to implement it in PBI with DAX.

 

I've the following table with several items. These items will always have a price, a currency, a validity date, a suppression code (if this price has been suppressed from the system for any reason) and a source. The table will be something like this (the given example only contains one item but in the real one I can have thousands):

 

    Item        Code         Date 1          Date 2        Currency       Value        Value USD        Source    
      1  01/07/2020 01/12/2024      USD    18.85        18.85      AB
      1  21/06/2022 21/12/2027      EUR     9.42        10.17      AB
      1  21/06/2022 21/12/2027      USD    19.00        19.00      AB
      1      A 14/04/2021 14/05/2023      USD     6.28         6.28      AC
      1  02/04/2020 02/12/2023      USD     6.28        6.28      AD
      1  01/01/2025 31/12/2027      USD     7.50        7.50      AD
      1      A 14/04/2021 14/05/2023      USD     6.70        6.70      AE
      1  02/04/2020 02/12/2023      USD     5.20        5.20      AE

 

The idea is to get a final price for each item and for each one of the sources following several rules (with a priority order):

 

  1. The first criterion will be the date: the final price must be valid today (Date 1 < Today < Date 2)
  2. If there are not valid date lines, a not valid date will be accepted (e.g. no lines available respecting the previous criterion)
  3. Lines with 'A' in column 'Code' will not be taken into account unless there is no other option (e.g. all lines for a given item and source are tagged 'A').
  4. The final price will be the average between the lines respecting the criteria using the 'Value USD' column

 

The result table will be something like this:

 

ItemValue (Source AB)Valid (Source AB)Value (Source AC)Valid (Source AC)Value (Source AD)Valid (Source AD)Value (Source AE)Valid (Source AC)Final ValueDispersion
114.59YesN/ACode A7.5Yes5.2Out of date9.42Warning

 

For each one of the sources, it will get back the average value for the valid lines respecting the previous criteria. The final price will always be selecting respecting the priority order if the source has a valid price (Source AB > Source AC > Source AD).

 

The column 'Valid (Source XX)' will be -'Yes', 'Code A', 'Out of date'- depending of the case used to compute the price. The last column will indicate that the prices used to calculate the average has a dispersion > 20%.

 

I'll try to upload my firsts ideas on how to solve it today but I would like to have your suggestions since I know some experts are around here 🙂

1 ACCEPTED SOLUTION
lucadelicio
Super User
Super User

Try

Measure =
var _a =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[Valid Date] = 1
    ,Foglio1[    Code    ] <> "A"
)

var _b =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[    Code    ] <> "A"
)

var _c =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
)

RETURN
IF(ISBLANK(_a) && ISBLANK(_b), _c
    ,IF(ISBLANK(_a), _b
        ,_a
    )
)


Second measure for the text:
Measure 2 =
var _a =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[Valid Date] = 1
    ,Foglio1[    Code    ] <> "A"
)

var _b =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[    Code    ] <> "A"
)

var _c =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
)

RETURN
IF(ISBLANK(_a) && ISBLANK(_b), "Code A"
    ,IF(ISBLANK(_a), "Out of dates"
        , "Yes"
    )
)

And put both into a matrix:
Row item
Columns: Source
lucadelicio_0-1717928124509.png




Luca D'Elicio

LinkedIn Profile

View solution in original post

5 REPLIES 5
lucadelicio
Super User
Super User

Try

Measure =
var _a =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[Valid Date] = 1
    ,Foglio1[    Code    ] <> "A"
)

var _b =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[    Code    ] <> "A"
)

var _c =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
)

RETURN
IF(ISBLANK(_a) && ISBLANK(_b), _c
    ,IF(ISBLANK(_a), _b
        ,_a
    )
)


Second measure for the text:
Measure 2 =
var _a =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[Valid Date] = 1
    ,Foglio1[    Code    ] <> "A"
)

var _b =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
    ,Foglio1[    Code    ] <> "A"
)

var _c =
CALCULATE(
    AVERAGE(Foglio1[    Value USD    ])
)

RETURN
IF(ISBLANK(_a) && ISBLANK(_b), "Code A"
    ,IF(ISBLANK(_a), "Out of dates"
        , "Yes"
    )
)

And put both into a matrix:
Row item
Columns: Source
lucadelicio_0-1717928124509.png




Luca D'Elicio

LinkedIn Profile
lucadelicio
Super User
Super User

As attachment the pbix file



Luca D'Elicio

LinkedIn Profile

It's not compatible with my version of PBI and I cannot install the last one 😞

lucadelicio
Super User
Super User

Hi @capko 
can you show tha calculation for the Value (Source AB)?
If i undestrand well it will be the avg of 18,85, 10,17 and 19,00. The valid line for item 1 source AB and the result will be 16,01.
I forgot something?
Why you are show 14,59?
Thank you



Luca D'Elicio

LinkedIn Profile

Hi ! You're completely right, it has been an error on my side 🙂

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.