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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
dianis
Employee
Employee

Calculate duration of a promotion

 

Dear community, 

 

I am looking to build a measure that allows me to calculate the average duration of a promotion, based on the parameters below:

cod_tienda is the store code

cod_semana is the week the promotion was active

cod_producto is product code

promotion is the promotion type

 

What I am looking for is to have a new column, that counts the number of weeks a promotion was active on that specific store and product. For instance:

- For Index 1, promotion was active for 1 week

- For indexes 4 to 7, the promotion was active for 4 weeks

- For Index 8, promotion was active for 1 week

- For Indexes 9 to 10, promotion was active for 2 weeks... and so one

 

At the end I want to understand what the average duration for a promotion is for an specific store and product.

dianis_0-1683734827568.png

 

Any hint on how to move forward?

 

Thanks!

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

Hi @dianis ,

You can create a calculated column as below to get it, please find the details in the attachment.

Average duration = 
VAR _minweek =
    CALCULATE (
        MIN ( 'Table'[cod_semana] ),
        FILTER (
            'Table',
            'Table'[cod_tienda] = EARLIER ( 'Table'[cod_tienda] )
                && 'Table'[cod_producto] = EARLIER ( 'Table'[cod_producto] )
                && 'Table'[promocion] = "Promo descuento"
        )
    )
VAR _maxweek =
    CALCULATE (
        MAX ( 'Table'[cod_semana] ),
        FILTER (
            'Table',
            'Table'[cod_tienda] = EARLIER ( 'Table'[cod_tienda] )
                && 'Table'[cod_producto] = EARLIER ( 'Table'[cod_producto] )
                && 'Table'[promocion] = "Promo descuento"
        )
    )
VAR _count =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[cod_semana] ),
        FILTER (
            'Table',
            'Table'[cod_tienda] = EARLIER ( 'Table'[cod_tienda] )
                && 'Table'[cod_producto] = EARLIER ( 'Table'[cod_producto] )
        )
    )
RETURN
    IF ( _count = 1, 1, ( _maxweek - _minweek + 1 ) / _count )

vyiruanmsft_0-1683858900553.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-yiruan-msft
Community Support
Community Support

Hi @dianis ,

You can create a calculated column as below to get it, please find the details in the attachment.

Average duration = 
VAR _minweek =
    CALCULATE (
        MIN ( 'Table'[cod_semana] ),
        FILTER (
            'Table',
            'Table'[cod_tienda] = EARLIER ( 'Table'[cod_tienda] )
                && 'Table'[cod_producto] = EARLIER ( 'Table'[cod_producto] )
                && 'Table'[promocion] = "Promo descuento"
        )
    )
VAR _maxweek =
    CALCULATE (
        MAX ( 'Table'[cod_semana] ),
        FILTER (
            'Table',
            'Table'[cod_tienda] = EARLIER ( 'Table'[cod_tienda] )
                && 'Table'[cod_producto] = EARLIER ( 'Table'[cod_producto] )
                && 'Table'[promocion] = "Promo descuento"
        )
    )
VAR _count =
    CALCULATE (
        DISTINCTCOUNT ( 'Table'[cod_semana] ),
        FILTER (
            'Table',
            'Table'[cod_tienda] = EARLIER ( 'Table'[cod_tienda] )
                && 'Table'[cod_producto] = EARLIER ( 'Table'[cod_producto] )
        )
    )
RETURN
    IF ( _count = 1, 1, ( _maxweek - _minweek + 1 ) / _count )

vyiruanmsft_0-1683858900553.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.