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

Dynamic comparison tables using separate date filters

Hi,
I have a main facts table that includes date, costs, clicks etc. 
I also have a separate dates table with a relationship setup with facts table, with elements such as date, YY_WW, YY_MM

I want to be able to generate a table with 3 columns. 
Period 1 totals, Period 2 totals, % difference. 

Then would have metrics as rows, such as clicks, cost

Then to use two use different date slicers so can dynamically change the sum for either period on selected value. Based on either 'date', 'YY_WW' or 'YY_MM'. 

So user could decide to compare 3 weeks ago vs. 6 weeks ago. So more dynamic than using something like this week vs. last week where one or both periods is more fixed. 

Possibly with use of things like 'selectedvalue', 'all', 'filter' within the measures, but not sure how to do. 

Any help appreciated. 
Thanks,
Chris


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

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

a1.png

 

Metrics:

a2.png

 

Calendar 1(a calculated table):

Calendar 1 = CALENDARAUTO()

 

Calendar 2(a calculated table):

Calendar 1 = CALENDARAUTO()

 

There is no relationship between tables. You may create two measures as below.

Period 1 totals = 
IF(
    HASONEVALUE(Metrics[Metric]),
    IF(
        SELECTEDVALUE(Metrics[Metric])="Cost",
        CALCULATE(
            SUM('Table'[Cost]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 1'[Date])
            )
        ),
        CALCULATE(
            SUM('Table'[Click]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 1'[Date])
            )
        )
    )
)
Period 2 totals = 
IF(
    HASONEVALUE(Metrics[Metric]),
    IF(
        SELECTEDVALUE(Metrics[Metric])="Cost",
        CALCULATE(
            SUM('Table'[Cost]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 2'[Date])
            )
        ),
        CALCULATE(
            SUM('Table'[Click]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 2'[Date])
            )
        )
    )
)

 

Result:

a3.png

 

Best Regards

Allan

 

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

7 REPLIES 7
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

If you take the answer of someone, please mark it as the solution to help the other member0s who have same problems find it more quickly. If not, let me know and I'll try to help you further. Thanks.

 


Best Regards

Allan

 

v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

You may try replacing 'ALL' with 'ALLSELECTED' to see if it works.

 

Best Regards

Allan

 

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

Anonymous
Not applicable

I kind of combined two above methods, but doesn't quite work as stuck on the first metric (impressions). Any idea what fix could be? Or if different way? Thanks for help!

P1 totals v2 = 

var cur_start=min(Dates_Table2[Date])

var cur_end=max(Dates_Table2[Date])

return

IF(

HASONEVALUE(Metrics[Metrics]),

IF(SELECTEDVALUE(Metrics[Metrics])="Impressions",

CALCULATE(

SUM('Analytics_Data_Presto'[Impressions]),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="Clicks",

CALCULATE(

SUM('Analytics_Data_Presto'[Clicks]),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="Costs",

CALCULATE(

SUM(Analytics_Data_Presto[Costs]),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="CTR",

CALCULATE(

(SUM('Analytics_Data_Presto'[Clicks])/sum(Analytics_Data_Presto[Impressions])*100),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="CPC",

CALCULATE(

(SUM('Analytics_Data_Presto'[Costs])/sum(Analytics_Data_Presto[Clicks])),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="Conversions",

CALCULATE(

SUM('Analytics_Data_Presto'[Conversions]),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="Conversion Value",

CALCULATE(

SUM('Analytics_Data_Presto'[Conversion Value]),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

IF(

HASONEVALUE(Metrics[Metrics]),

If(selectedvalue(Metrics[Metrics])="CVR",

CALCULATE(

(SUM('Analytics_Data_Presto'[Conversions])/sum(Analytics_Data_Presto[Clicks])*100),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

),

CALCULATE(

(SUM('Analytics_Data_Presto'[Conversion Value])/sum(Analytics_Data_Presto[Conversions])),

Analytics_Data_Presto[Date]>=cur_start && Analytics_Data_Presto[Date]<=cur_end)

)

)

)

)))))
v-alq-msft
Community Support
Community Support

Hi, @Anonymous 

 

Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.

Table:

a1.png

 

Metrics:

a2.png

 

Calendar 1(a calculated table):

Calendar 1 = CALENDARAUTO()

 

Calendar 2(a calculated table):

Calendar 1 = CALENDARAUTO()

 

There is no relationship between tables. You may create two measures as below.

Period 1 totals = 
IF(
    HASONEVALUE(Metrics[Metric]),
    IF(
        SELECTEDVALUE(Metrics[Metric])="Cost",
        CALCULATE(
            SUM('Table'[Cost]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 1'[Date])
            )
        ),
        CALCULATE(
            SUM('Table'[Click]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 1'[Date])
            )
        )
    )
)
Period 2 totals = 
IF(
    HASONEVALUE(Metrics[Metric]),
    IF(
        SELECTEDVALUE(Metrics[Metric])="Cost",
        CALCULATE(
            SUM('Table'[Cost]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 2'[Date])
            )
        ),
        CALCULATE(
            SUM('Table'[Click]),
            FILTER(
                ALL('Table'),
                [Date] in DISTINCT('Calendar 2'[Date])
            )
        )
    )
)

 

Result:

a3.png

 

Best Regards

Allan

 

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

Anonymous
Not applicable

Thanks for this!
It works well, except I can't use filters on the data. I guess due to the ALL function, or because the metrics table isn't connected to the main facts table. Is there a way around this, so I could filter by account_name for example in the facts table. At moment, if I do filter for this, it just gives overall total, and is same for each account_name. 
Thanks,
Chris

Anonymous
Not applicable

relationship with main table and dates tables

power bi forum.PNG

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.