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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Jlindsey00
Advocate I
Advocate I

Average The Sum of Two Columns When Neither Are Blank Over 'x' Amount of Weeks

Hi everyone,

 

I have been working on this problem for over a week and keep getting stuck. 

 

Problem to Solve

- Divide the sum of SalesAmount by the sum of LeadQty for an AgentId in the last 4 Working Weeks. Essentially the four-week rolling average with some caveats.

 

Notes and Specifics

- I want to calculate only those dates (WeekEndDate) where both SalesAmount and LeadQty are not Blank.

- So a Working Week is a week where SalesAmount and/or LeadQty has a value

- One of the issues I'm running into is my measure counting a Non-Work Week as a Working Week.

- In the Sample Data below you'll see the calculation sums up the SalesAmount for each Working Week (highlighted) even if SalesAmount is Blank. This is because for that week LeadQty is not Blank. So we have 3 values for SalesAmount in those 4 Working Weeks. The same is true for Lead Qty, we are summing 3 values in the 4 Working Weeks.

 

DAX Measure

 

RollingAvg_4Week = 
VAR StartDate =
    FIRSTNONBLANK (
        'Calendar'[WeekEndDate],
        OR (
            SUM ( Sales[SalesAmount] ) > 0,
            SUM ( Leads[LeadQty] ) > 0
        )
    )
VAR EndDate =
    LASTNONBLANK (
        DATEADD ( 'Calendar'[WeekEndDate], -7, DAY ),
        OR (
            SUM ( Sales[SalesAmount] ) > 0,
            SUM ( Leads[LeadQty] ) > 0
        )
    )
RETURN
    CALCULATE (
        DIVIDE (
            SUM ( Sales[SalesAmount] ),
            SUM ( Leads[LeadQty] )
        ),
        DATESINPERIOD ( 'Calendar'[WeekEndDate], EndDate, -28, DAY )
    )

 

Sample Data in Excel

Screenshot 2024-06-25 at 9.56.31 AM.png

 

 

 

 

 

 

Power BI Model

- Agents Table w/ columns AgentId, Name

- Sales Table w/ columns AgentId, SalesAmount, SubmittedDate, WeekEndDate

- Lead Table w/ columns AgentId, LeadQty, WeekEndDate

- Calendar Table w/ columns Date, WeekEndDate

- Relationships: Calendar[Date] to Sales[SubmittedDate] and to Leads[WeekEndDate]

-- Agents[AgentId] to Sales[AgentId] and to Leads[AgentId]

 

Thank you all!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Jlindsey00 ,

 

Please try:

 

Create a measure:

Measure = 
VAR CurrentDate = TODAY()
VAR StartDate = CurrentDate - 28
VAR EndDate = CurrentDate
VAR ValidSales = 
    CALCULATE(
        SUM('Table'[SaleAmount]),
        'Table'[SaleAmount] <> BLANK(),
        'Table'[LeadQty] <> BLANK(),
        FILTER(
            ALL('Date'),
            'Date'[Date] >= StartDate && 'Date'[Date] <= EndDate
        )
    )
VAR ValidLeads = 
    CALCULATE(
        SUM('Table'[LeadQty]),
        'Table'[SaleAmount] <> BLANK(),
        'Table'[LeadQty] <> BLANK(),
        FILTER(
            ALL('Date'),
            'Date'[Date] >= StartDate && 'Date'[Date] <= EndDate
        )
    )
RETURN
IF(ValidSales <> BLANK() && ValidLeads <> BLANK(), ValidSales / ValidLeads, BLANK())

 

The final page visual effect is as follows:

vhuijieymsft_0-1719366549763.png

 

If you have any other questions please feel free to contact me.

 

The pbix file is attached.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Jlindsey00 ,

 

Please try:

 

Create a measure:

Measure = 
VAR CurrentDate = TODAY()
VAR StartDate = CurrentDate - 28
VAR EndDate = CurrentDate
VAR ValidSales = 
    CALCULATE(
        SUM('Table'[SaleAmount]),
        'Table'[SaleAmount] <> BLANK(),
        'Table'[LeadQty] <> BLANK(),
        FILTER(
            ALL('Date'),
            'Date'[Date] >= StartDate && 'Date'[Date] <= EndDate
        )
    )
VAR ValidLeads = 
    CALCULATE(
        SUM('Table'[LeadQty]),
        'Table'[SaleAmount] <> BLANK(),
        'Table'[LeadQty] <> BLANK(),
        FILTER(
            ALL('Date'),
            'Date'[Date] >= StartDate && 'Date'[Date] <= EndDate
        )
    )
RETURN
IF(ValidSales <> BLANK() && ValidLeads <> BLANK(), ValidSales / ValidLeads, BLANK())

 

The final page visual effect is as follows:

vhuijieymsft_0-1719366549763.png

 

If you have any other questions please feel free to contact me.

 

The pbix file is attached.

 

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

Thank you @Anonymous , however this is not exactly what I was aiming for. 

 

In you example, here is what should be added and averaged:

 

Dates 6/20, 6/6, 5/30 and 5/16 as they are the first four dates with either a SalesAmount or LeadQty.

 

So the sales amounts that should be summed are 1063 (6/20), 139 (5/30) and 437 (5/16).

And the lead quantities that should be summed are 18 (6/20), 20 (6/6) and 20 (5/30).

 

Given this information, how would you modify the measure?

 

Thank you

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.