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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
veeranc
Frequent Visitor

DAX- Pre and Post Category averages on specific date

Hi everyone, 

 

I am struggling with DAX for this poblem, 

Data looks like this (4th column I added for reference)

DateCategoryValue 
1/1/2023A12 
1/3/2023D22 
1/6/2023A34 
1/7/2023D19Pre 2
1/8/2023A7 
1/10/2023S22 
1/12/2023A34 
1/27/2023S12Pre 2
2/14/2023D13Pre 1
4/3/2023A24Pre 2
4/5/2023S16Pre 1
4/7/2023A13Pre 1
4/18/2023D11Post 1
5/1/2023A5Post 1
5/2/2023S15Post 1
5/2/2023S4Post 2
5/3/2023D7Post 2
5/4/2023A5Post 2
5/23/2023D15 
5/30/2023S21 

 

On any specific day which is in another table for example 4/8/23 for A, D and S. They could also say just 5/10/23 for A and D

 

I need Pre and Post averages of 2 values, then calculate the difference

CategoryADS
Previous 218.51614
Post 25149.5
Difference13.524.5

 

any help is appreciated

4 REPLIES 4
veeranc
Frequent Visitor

Thanks @Anonymous 
I guess my problem was not clear. I could not attach the pbix file with data which would have been easier.

I Have two Tables - TableSales and TablePromos

TableSales has Product, Category, SaleDate and SaleAmount

TablePromos has  Product and PromoDates

 

At any Promo date slected, I wanted the difference of sales average for each category previous 2 days to following 2 days that Product.

User first chooses Product and then selects listed PromDate

Result should be table - Category, Prepromo, PostPromo and difference

 

I am trying to Rank based on Promodate and then pick 2 values pre and post for averages. I am getting errors.

 

Appreciate your help.

Thanks

Veera

Anonymous
Not applicable

Hi @veeranc,

If you mean to list the previous and post average based on selection, you can take a look at the following measure formula:

formula =
VAR newCategory =
    SELECTEDVALUE ( NewTable[NewColumn] )
VAR currDate =
    MAX ( Table1[PromDate] )
VAR prev =
    CALCULATE (
        AVERAGE ( Table1[Values] ),
        FILTER ( ALLSELECTED ( Table1 ), [Date] >= currDate - 2 && [Date] < currDate ),
        VALUES ( Table1[Category] )
    )
VAR post =
    CALCULATE (
        AVERAGE ( Table1[Values] ),
        FILTER ( ALLSELECTED ( Table1 ), [Date] > currDate && [Date] <= currDate + 2 ),
        VALUES ( Table1[Category] )
    )
RETURN
    SWITCH ( newCategory, "Previous", prev, "Post", post, prev - post )

You also need a new table with different type strings to use as matrix row and expand the calcations, raw table category field as column.

Regards,

Xiaoxin Sheng

Hi @Anonymous 

Thanks for the solution, I arrived at similar solution. If sale dates are continous then this works. If there are holidays or weekends or some other reason, then 2 day calculation won't work. I have to dynamically pick last two sale dates. That's where I am stuck now. That's the reason for trying out RANK.

 

Thanks

Veera

Anonymous
Not applicable

HI @veeranc,

Perhaps you can try to use the following measure formula to use the new category column as condition to redirect to different calculation expressions:

formula =
VAR newCategory =
    SELECTEDVALUE ( Table1[NewColumn] )
RETURN
    IF (
        newCategory <> "Difference",
        CALCULATE (
            AVERAGE ( Table1[Values] ),
            FILTER (
                ALLSELECTED ( Table1 ),
                SEARCH (
                    SWITCH ( newCategory, "Previous", "Pre", "Post", "Post", "" ),
                    Table1[NewColumn],1,-1
                    ) > 0
            ),
            VALUES ( Table1[Category] )
        ),
        CALCULATE (
            CALCULATE (
                AVERAGE ( Table1[Values] ),
                SEARCH ( "Pre", Table1[NewColumn], 1, -1 ) > 0
            )
                - CALCULATE (
                    AVERAGE ( Table1[Values] ),
                    SEARCH ( "Post", Table1[NewColumn], 1, -1 ) > 0
                ),
            ALLSELECTED ( Table1 ),
            VALUES ( Table1[Category] )
        )
    )

Regards,

Xiaoxin Sheng

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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