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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
HYEasterly
Frequent Visitor

IF BLANK RETURN AVERAGE OF PAST 3 WEEKS

Let me start with saying I have read many posts that are similar to my question... and I have attempted many of those solutions.  I have not gotten the result I want... so here I am asking for help.

 

Goal:  IF the visual returns a blank for the Total Labor Cost THEN RETURN the average of the prior 3 weeks Total Labor Cost.  (We get revenue numbers in real time... but labor costs  have to wait on payroll to be ran the following week.  We want an average of the labor costs to show until the actual labor cost is provided.)

 

This is a snip of my visual.  Notice how May 18 is blank in the Labor column... 

HYEasterly_1-1747857087375.png

 

My data is very complex and has multiple tables.  I have condensed it down to the tables and fields I think I need to use.

Table Name:Labor Data
FieldType
Period End DateData
Total Labor CostDAX
Department NumberData
WagesData
TaxesData
BenefitsData
Prior Week 1DAX
Prior Week 2DAX
Prior Week 2DAX
  
Table Name:Revenue Data
FieldType
Period End DateData
Department NumberData
RevenueData
  
Table Name:Period Ending
FieldType
Period End DateData
Prior Week 1Data
Prior Week 2Data
Prior Week 2Data
  
Table Name:Department Data
FieldType
Department NumberData
Department NameData

 

The Prior Week fields were added while trying to get this to work.    

How do you suggest I accomplish getting it to return the average of the prior 3 weeks labor cost if there is no labor cost for a specific week?

 

Thank you in advance!

1 ACCEPTED SOLUTION

Hi @HYEasterly,

I have  created a solution that addresses your requirements, including employee-level data, department slicer, and averaging the prior 3 weeks’ labor costs when the current week is blank.

create the following DAX Measures to ensure accurate results:

Total Labor Cost = 
SUM('Labor Data'[Labor Cost])

 

Avg Labor Last 3 Weeks = 
VAR CurrentPeriod = MAX('Period Table'[Period End Date])
VAR Department = SELECTEDVALUE('Labor Data'[Department Number])

VAR PreviousPeriods =
    TOPN(
        3,
        FILTER(
            ALL('Period Table'),
            'Period Table'[Period End Date] < CurrentPeriod
        ),
        'Period Table'[Period End Date], DESC
    )

VAR Result =
    AVERAGEX(
        PreviousPeriods,
        CALCULATE(
            [Total Labor Cost],
            'Period Table'[Period End Date] = EARLIER('Period Table'[Period End Date]),
            'Labor Data'[Department Number] = Department
        )
    )

RETURN Result

 

Labor with Fallback = 
VAR Actual = [Total Labor Cost]
RETURN IF(
    ISBLANK(Actual),
    [Avg Labor Last 3 Weeks],
    Actual
)


I was able to achieve the expected output as per your requirement.
Please refer to the screenshot and attached pbix file below showing the correct results:

vhjannapu_0-1748323595464.png

If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.

Best Regards,
Harshitha.
Community support Team.




View solution in original post

5 REPLIES 5
v-hjannapu
Community Support
Community Support

Hi @HYEasterly,

Thank you for reaching out to the Microsoft fabric community forum.
I would also take a moment to thank @pankajnamekar25 , for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

I created your scenario using simplified sample data. I manually added the tables (Labor Data, Revenue Data, and a Calendar table), established the necessary relationships (based on Period End Date), and then implemented the DAX measure to return the average of the prior 3 weeks' labour cost when the current week is blank. I visualized the result using a Matrix visual, and it returned the expected values.

Please find the attached pbix file and screenshot for your reference.

vhjannapu_1-1747890743418.png

If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.

Best Regards,
Harshitha.
Community support Team.

@v-hjannapu @pankajnamekar25 

Thank you both for your answers.  

 

My data is broken down by department and employee.  My visual also has a slicer so the user can look at certain departments.  The simplified version you provided doesn't account for either of these.  It returns the 3 largest paychecks from the last 3 weeks not the entire payroll for the last 3 weeks.

 

I'm still working with it to try to get my data.

 

Hi @HYEasterly,

I have  created a solution that addresses your requirements, including employee-level data, department slicer, and averaging the prior 3 weeks’ labor costs when the current week is blank.

create the following DAX Measures to ensure accurate results:

Total Labor Cost = 
SUM('Labor Data'[Labor Cost])

 

Avg Labor Last 3 Weeks = 
VAR CurrentPeriod = MAX('Period Table'[Period End Date])
VAR Department = SELECTEDVALUE('Labor Data'[Department Number])

VAR PreviousPeriods =
    TOPN(
        3,
        FILTER(
            ALL('Period Table'),
            'Period Table'[Period End Date] < CurrentPeriod
        ),
        'Period Table'[Period End Date], DESC
    )

VAR Result =
    AVERAGEX(
        PreviousPeriods,
        CALCULATE(
            [Total Labor Cost],
            'Period Table'[Period End Date] = EARLIER('Period Table'[Period End Date]),
            'Labor Data'[Department Number] = Department
        )
    )

RETURN Result

 

Labor with Fallback = 
VAR Actual = [Total Labor Cost]
RETURN IF(
    ISBLANK(Actual),
    [Avg Labor Last 3 Weeks],
    Actual
)


I was able to achieve the expected output as per your requirement.
Please refer to the screenshot and attached pbix file below showing the correct results:

vhjannapu_0-1748323595464.png

If the response has addressed your query, please Accept it as a solution and give a 'Kudos' so other members can easily find it.

Best Regards,
Harshitha.
Community support Team.




Ashish_Excel
Resolver IV
Resolver IV

Hi,

This pattern should work

Measure = if(isblank([Total labour cost]),calculate([total labour cost],datesbetween(calendar[date],min(calendar[date])-21,min(calendar[date])-1)),[total labour cost])

pankajnamekar25
Memorable Member
Memorable Member

Hello @HYEasterly 

 

Try this

Total Labor Cost - Adjusted :=

VAR CurrentWeek = SELECTEDVALUE('Labor Data'[Period End Date])

VAR CurrentLabor = SELECTEDVALUE('Labor Data'[Total Labor Cost])

 

VAR PreviousWeeks =

    CALCULATETABLE (

        VALUES('Labor Data'[Total Labor Cost]),

        FILTER (

            ALL('Labor Data'),

            'Labor Data'[Period End Date] < CurrentWeek

        ),

        TOPN(3,

            FILTER (

                ALL('Labor Data'),

                'Labor Data'[Period End Date] < CurrentWeek &&

                NOT ISBLANK('Labor Data'[Total Labor Cost])

            ),

            'Labor Data'[Period End Date], DESC

        )

    )

 

VAR AvgLast3 =

    AVERAGEX(PreviousWeeks, [Total Labor Cost])

 

RETURN

    IF (

        ISBLANK(CurrentLabor),

        AvgLast3,

        CurrentLabor

    )

 

 

You could alternatively use those columns in the measure instead

Total Labor Cost - Adjusted :=

VAR CurrentLabor = [Total Labor Cost]

VAR Prior1 = [Prior Week 1]

VAR Prior2 = [Prior Week 2]

VAR Prior3 = [Prior Week 3]

 

VAR AvgPrior3 = AVERAGEX(

    { Prior1, Prior2, Prior3 },

    [Value]

)

 

RETURN

    IF (

        ISBLANK(CurrentLabor),

        AvgPrior3,

        CurrentLabor

    )

But this is less dynamic  using the first method with date filtering is more scalable.

 

 

 

Thanks,
 Pankaj Namekar | LinkedIn

If this solution helps, please accept it and give a kudos (Like), it would be greatly appreciated.

Helpful resources

Announcements
May PBI 25 Carousel

Power BI Monthly Update - May 2025

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

May 2025 Monthly Update

Fabric Community Update - May 2025

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