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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Calculation of dynamic price differences between sites

Hello

I need to calculate price differences for a particular site weighted by kilos.

From the example that is attached, I require calculating the sum dif column of price differences for a site multiplied by the kilos.

The complexity is that the result of the dif column has to be dynamic for N sites. Sites can vary as different filter combinations are used.

The other thing that the price value I have is built in a single measure, which makes it difficult for me to perform the subtraction.

diferencias_precio_entre_sitios.png

I'll appreciate your help.

In advance thank you.

Mario Ortiz Lazo

Prime-e

3 REPLIES 3
v-shex-msft
Community Support
Community Support

HI @Anonymous,

I'm not so sure for your table structure, can you please share some dummy data to test?

How to Get Your Question Answered Quickly 

In addition, you can try to use the following measure formula if it suitable for your requirement:

Measure =
VAR currkilos =
    CALCULATE (
        SUM ( table[value] ),
        FILTER ( table, [site] = "kilos" ),
        VALUES ( table[rango] ),
        VALUES ( table[sitio] )
    )
VAR summary =
    SUMMARIZE (
        FILTER ( table, [site] <> "kilos" && [site] <> "diff" ),
        [rango],
        [sitio],
        [site],
        "total", SUM ( table[value] )
    )
RETURN
    SUMX ( summary, currkilos - [total] ) * currkilos

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Anonymous
Not applicable

Thank you for the help, I have not yet been able to resolve it with the formula sent.

The table structure is as follows:

The price comes from the calculation of a measure from a table above.

SiteKilosPriceRange
Site 20,324986560,77329291>32
Site 20,189448230,1022139628-30
Site 20,324413270,7701182426-28
Site 20,967942020,0721012424-25
Site 20,965300410,0174307522-24
Site 20,546408710,324289426-28
Site 20,307540430,0119099322-24
Site 20,749800470,9553955528-30
Site 20,374609990,4909390924-25
Site 20,515869520,7382535524-25
Site 30,689697970,125086624-25
Site 30,239506370,4456248226-28
Site 30,86191030,2729325428-30
Site 30,876825040,8293345722-24
Site 30,166641130,7776033824-25
Site 30,534397380,0665841828-30
Site 30,014460910,6140501726-28
Site 30,343231650,8342163522-24
Site 30,991000390,7215028124-25
Site 30,157340910,8088605228-30
Site 10,07809610,5629178828-30
Site 10,782759470,0247830324-25
Site 10,832666730,6946093126-28
Site 10,461078760,2868927222-24
Site 10,79346750,68517226>32
Site 10,512156350,0301170426-28
Site 10,24738330,6720060922-24
Site 10,841542610,4453163924-25
Site 10,280318810,93173736>32
Site 10,19534790,4593276828-30

Best regards

Mario Ortiz

Hello @Mortiz,

You can refer to the following steps to achieve your requirements.

1. Make an un pivoted column in the Pirce and Kilos fields.

Un pivoted columns (power query)

2. Create an array visual with the range as row, site, type (un pivoted attribute field) as column.

3. Type a measurement formula to use its row content to calculate the result and use it in the array value fields.

Measure = 
VAR currKilo =
    CALCULATE (
        SUM ( T2[Value] ),
        FILTER ( ALLSELECTED ( T2 ), T2[Type] = "Kilos" ),
        VALUES ( T2[Range] ),
        VALUES ( T2[Site] )
    )
VAR price =
    CALCULATE (
        SUM ( T2[Value] ),
        FILTER ( ALLSELECTED ( T2 ), T2[Type] = "Price" ),
        VALUES ( T2[Range] ),
        VALUES ( T2[Site] )
    )
VAR siteCount =
    COUNTROWS ( EXCEPT ( ALLSELECTED ( T2[Site] ), VALUES ( T2[Site] ) ) )
VAR totalPrice =
    CALCULATE (
        SUM ( T2[Value] ),
        FILTER (
            ALLSELECTED ( T2 ),
            NOT ( [Site] IN VALUES ( T2[Site] ) )
                && [Type] = "Price"
        ),
        VALUES ( T2[Range] )
    )
RETURN
    IF (
        ISFILTERED ( T2[Type] ),
        SUM ( T2[Value] ),
        ( price * siteCount - totalPrice ) * currKilo
    )

4. Add a sort table to extract the range value and add 'order' fields, then set the order fields as the 'sort by column' range field and use it to replace the array range field.

Sort by column in Power BI Desktop Custom sorting in Power BI

Sort = 
ADDCOLUMNS (
    VALUES ( T2[Range] ),
    "Order",
    VAR result =
        PATHITEM ( SUBSTITUTE ( [Range], "-", "|" ), 1 )
    RETURN
        IF (
            SEARCH ( ">", result, 1, -1 ) > 0,
            RIGHT ( result, LEN ( result ) - 1 ),
            result
        )
)

Design and result of the matrix:

3.png

BTW, I also attached the sample file below if you are confused about these steps.

Best regards

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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