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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
linusb
Frequent Visitor

Mida para sumar fila por fila, en función del precio de compra más reciente.

Hola

Estoy tratando de crear una medida que calcula el "Volumen de compra" (Precio actual * Demanda) por artículo y proveedor para un mes determinado.

He configurado tres tablas (Precio de compra, Supplers y DemandR12), ver ejemplo a continuación:

La tabla PurchasePrice incluye entradas cada vez que cambia un precio de un artículo:

ItemNoFechaPrecio
A2020-11-01 1
B2020-10-01 2
C2020-09-01 3
A2020-09-01 2
B2020-06-01 4
C2020-05-01 6

Tabla de proveedores

ProveedorItemNo
Proveedor X A
Proveedor X B
Proveedor Y C

Tabla DemandR12

ItemNoDemandR12
A 100
B 150
C 200

Para calcular el volumen de compra por artículo para noviembre y septiembre estoy seleccionando el último precio de compra (del mes seleccionado o más) * DemandR12:

Total sales volume November 2020
Item A => 1 * 100 = 100 (Supplier X)
Item B => 2 * 150 = 300 (Supplier X)
Item C => 3 * 200 = 600 (Supplier Y)
Total = 1000
Total sales volume September 2020
Item A => 2 * 100 = 200 (Supplier X)
Item B => 4 * 150 = 600 (Supplier X)
Item C => 6 * 200 = 1200 (Supplier Y)
Total = 2000

También quiero poder calcular el volumen total de compra por proveedor:

Total sales volume per Supplier November 2020
Supplier X => 1*100 + 2* 150 = 400
Supplier Y => 3 * 200 = 600
Total sales volume per Supplier September 2020
Supplier X => 2*100 + 4* 150 = 800
Supplier Y => 6 * 200 = 1200

He podido crear una medida (ver más abajo) que calcula el volumen de compra por artículo, pero si quiero mostrarlo por proveedor no funciona según lo previsto. Supongo que mi variable _MaxDate seleccionará la fecha máxima para el proveedor seleccionado.
Es decir: Si el precio del artículo A cambió 2020-11-01 y el precio del artículo B 2020-10-01, entonces el _MaxDate será 2020-11-01 para el Proveedor X en noviembre de 2020. Por lo tanto, la suma del precio de la partida B es 0, por lo tanto, la fecha del artículo B es 2020-10-01.

Purchase Volume v2 = 
Var _MaxDate = 
    // Max date for selected row
        MAX(Dates[Date])

VAR _LatestPriceChangeDate =
        CALCULATE(
            MAX('Price Development Overtime'[Date]),
            ALLSELECTED(Dates),
            Dates[Date] <= _MaxDate
        )

var _CurrentPrice =    CALCULATE(
        Sumx('Price Development Overtime', 'Price Development Overtime'[Price] * [Qty used]),
        ALL(Dates[Date]),
        Dates[Date] = _LatestPriceChangeDate
            )

var _Result =
        _CurrentPrice
Return
    _Result

Podría cambiar mi procedimiento SQL para crear una entrada por mes, incluso si el precio no ha cambiado, pero eso dará lugar a una tabla muy grande.

Cualquier ayuda será muy apreciada

Br

Linus

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hola @linusb,

Puede intentar utilizar las siguientes fórmulas de medida si cumplen con sus requisitos:

Total Sales=
VAR selected =
    MAX ( Date[Date] )
VAR summary =
    ADDCOLUMNS (
        SUMMARIZE (
            DemandR12,
            [ItemNo],
            [DemandR12],
            "Price",
                VAR maxDate =
                    MAXX (
                        FILTER (
                            ALLSELECTED ( PurchasePrice ),
                            [ItemNo] = EARLIER ( [ItemNo] )
                                && [Date] <= selected
                        ),
                        [Date]
                    )
                RETURN
                    LOOKUPVALUE (
                        PurchasePrice[Price],
                        PurchasePrice[ItemNo], [ItemNo],
                        PurchasePrice[Date], maxDate
                    )
        ),
        "Total", [DemandR12] * [Price]
    )
RETURN
    SUMX ( summary, [Total] )

Total Sales Per Supplier =
VAR selected =
    MAX ( Date[Date] )
VAR _list =
    CALCULATETABLE ( VALUES ( Supplier[ItemNo] ), ALLSELECTED ( Supplier ) )
VAR summary =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( ALLSELECTED ( DemandR12 ), [ItemNo] IN _list ),
            [ItemNo],
            [DemandR12],
            "Price",
                VAR maxDate =
                    MAXX (
                        FILTER (
                            ALLSELECTED ( PurchasePrice ),
                            [ItemNo] = EARLIER ( [ItemNo] )
                                && [Date] <= selected
                        ),
                        [Date]
                    )
                RETURN
                    LOOKUPVALUE (
                        PurchasePrice[Price],
                        PurchasePrice[ItemNo], [ItemNo],
                        PurchasePrice[Date], maxDate
                    )
        ),
        "Total", [DemandR12] * [Price]
    )
RETURN
    SUMX ( summary, [Total] )

saludos

Xiaoxin Sheng

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

View solution in original post

2 REPLIES 2
v-shex-msft
Community Support
Community Support

Hola @linusb,

Puede intentar utilizar las siguientes fórmulas de medida si cumplen con sus requisitos:

Total Sales=
VAR selected =
    MAX ( Date[Date] )
VAR summary =
    ADDCOLUMNS (
        SUMMARIZE (
            DemandR12,
            [ItemNo],
            [DemandR12],
            "Price",
                VAR maxDate =
                    MAXX (
                        FILTER (
                            ALLSELECTED ( PurchasePrice ),
                            [ItemNo] = EARLIER ( [ItemNo] )
                                && [Date] <= selected
                        ),
                        [Date]
                    )
                RETURN
                    LOOKUPVALUE (
                        PurchasePrice[Price],
                        PurchasePrice[ItemNo], [ItemNo],
                        PurchasePrice[Date], maxDate
                    )
        ),
        "Total", [DemandR12] * [Price]
    )
RETURN
    SUMX ( summary, [Total] )

Total Sales Per Supplier =
VAR selected =
    MAX ( Date[Date] )
VAR _list =
    CALCULATETABLE ( VALUES ( Supplier[ItemNo] ), ALLSELECTED ( Supplier ) )
VAR summary =
    ADDCOLUMNS (
        SUMMARIZE (
            FILTER ( ALLSELECTED ( DemandR12 ), [ItemNo] IN _list ),
            [ItemNo],
            [DemandR12],
            "Price",
                VAR maxDate =
                    MAXX (
                        FILTER (
                            ALLSELECTED ( PurchasePrice ),
                            [ItemNo] = EARLIER ( [ItemNo] )
                                && [Date] <= selected
                        ),
                        [Date]
                    )
                RETURN
                    LOOKUPVALUE (
                        PurchasePrice[Price],
                        PurchasePrice[ItemNo], [ItemNo],
                        PurchasePrice[Date], maxDate
                    )
        ),
        "Total", [DemandR12] * [Price]
    )
RETURN
    SUMX ( summary, [Total] )

saludos

Xiaoxin Sheng

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

Gracias por los comentarios. ¡La primera medida hizo el truco!

Linus

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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

Top Solution Authors