filterpowerbiaverage

Power BI Average measure seems to ignore dates


I am new to both Power BI and Stack Overflow, but I am having a bit of trouble getting an AVERAGE measure to work on my data in Power BI.

I have a table of products with pricing for myself and the difference in price for two competitors, and I have a date slicer which uses the date of the corresponding Monday for each date. I want my table to show the average price difference for each competitor during the week selected in the slicer.

The problem is that upon selecting a week date in the slicer, the average values for Competitor 1 and Competitor 2 are not correct.

I have created a measure for each competitor that looks like this:

Most Recent Diff Competitor 1 = 
VAR MinDate = MIN('Calendar'[WeekStartDate])
VAR MostRecentDate = 
    CALCULATE(
        MAX(pricetracker_price_history[date]),
        REMOVEFILTERS('Calendar'[WeekStartDate]),
        pricetracker_price_history[date]<MinDate,
        pricetracker_price_history[Competitor 1]<>BLANK()
    )
VAR MostRecentDiff = 
    IF(
        AVERAGE(pricetracker_price_history[Competitor 1])<>BLANK(),
        AVERAGE(pricetracker_price_history[Competitor 1]),
        IF(MostRecentDate<>BLANK(),
            CALCULATE(
                AVERAGE(pricetracker_price_history[Competitor 1]),
                REMOVEFILTERS('Calendar'[WeekStartDate]),
                pricetracker_price_history[date]=MostRecentDate
            ),
            COUNTBLANK(pricetracker_price_history[Competitor 1])-COUNTBLANK(pricetracker_price_history[Competitor 1]) //This was to give 0 instead of blank spaces
        )
    )
RETURN MostRecentDiff

The idea is that if there is no price difference given in the table for a particular date, then the price difference should be taken as the most recent non-blank value prior to that date.

My tables look like this (truncated here):

pricetracker_price_history

sku Date My Price Competitor 1 Competitor 2
12345 30/07/2024 £52 -£0.05 -£2.01
12345 29/07/2024 £52 -£0.05 -£2.01
12345 28/07/2024 £52 -£0.05 -£2.01
12345 27/07/2024 £52 -£0.05 -£2.01
12345 26/07/2024 £52 -£0.05 -£2.01
12345 25/07/2024 £52 -£0.05 -£2.01
12345 24/07/2024 £52 -£0.05 -£2.01
12345 23/07/2024 £52 -£0.05 -£2.01
12345 22/07/2024 £52 -£0.05 -£2.01
12345 21/07/2024 £52 -£0.05 -£2.01
12345 20/07/2024 £52 -£0.05 -£2.01
12345 19/07/2024 £52 -£0.05 -£2.01
12345 18/07/2024 £52 -£0.05 -£2.01
12345 17/07/2024 £52 -£0.05 -£2.01
12345 16/07/2024 £52 -£0.05 -£2.01
12345 15/07/2024 £52 -£5.06
12345 14/07/2024 £52 -£5.06
12345 13/07/2024 £52 -£5.06

Calendar

Date WeekStartDate
30/07/2024 29/07/2024
29/07/2024 29/07/2024
28/07/2024 22/07/2024
27/07/2024 22/07/2024
26/07/2024 22/07/2024
25/07/2024 22/07/2024
24/07/2024 22/07/2024
23/07/2024 22/07/2024
22/07/2024 22/07/2024
21/07/2024 15/07/2024
20/07/2024 15/07/2024
19/07/2024 15/07/2024
18/07/2024 15/07/2024
17/07/2024 15/07/2024
16/07/2024 15/07/2024
15/07/2024 15/07/2024
14/07/2024 08/07/2024
13/07/2024 08/07/2024

Upon selecting WeekStartDate as 22/07/2024 in the slicer, the visualisation gives:

sku Competitor 1 Average Competitor 2 Average
12345 -£2.06 -£4.45

when I would expect it to be

sku Competitor 1 Average Competitor 2 Average
12345 -£0.05 -£2.01

I'm struggling to see why the average is not being taken over just the dates with WeekStartDate = 22/07/2024.

Any help would be greatly appreciated!


Solution

  • I think you should get the correct answer. Have you create the relationship between two tables (Connecting date columns)

    enter image description here

    Then create two measures

    Measure = AVERAGE(pricetracker_price_history[Competitor 1])
    measure2 = average(pricetracker_price_history[Competitor 2])
    

    enter image description here