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
arifulice
Helper I
Helper I

how to find Last 12 days average egg production from Production Table

Dear all,

I have a production table with several columns, I want to find out the average egg production of last 12 days.

I have written the following code, but the result giving the data of all month and year. what is the problem

EggProductionOfLast12Days :=
CALCULATE (
    AVERAGE ( FactProductionData[Egg Prod#Qty] ),
    FILTER (
        FactProductionData,
        FactProductionData[Date] <= LASTDATE ( FactProductionData[Date] )
            && FactProductionData[Date]
                >= LASTDATE ( FactProductionData[Date] ) - 12
    )
)

 

7 REPLIES 7
Ashish_Mathur
Super User
Super User

Hi @arifulice,

 

Try this

 

  1. Create a calendar table with dates spanning the ones you have in your FactProductionData table
  2. Create a relationship from the Date column of your FactProductionData table to the Date column of your Calendar table
  3. Drag the Date column from your calendar table to filter/slicer section of your visual
  4. Select a date in the filter/slicer section
  5. Write this calculated field formula

=CALCULATE(AVERAGE(FactProductionData[Egg Prod#Qty]),DATESBETWEEN(Calendar[Date],MIN(Calendar[Date])-11,MAX(Calendar[Date])))

 

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
MFelix
Super User
Super User

Hi @arifulice,

Try to change the LASTDATE by
DATEADD( FactProduction[Date], -12, DAY)

Regards,
MFelix

Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



Hello MFelix ,

 

After changing the lastdate by the followiung code, the result is remaining the same like giving the result for month Jan to December. 

EggProductionOfLast12Days:=CALCULATE (
    AVERAGE ( FactProductionData[Egg Prod#Qty] ),
    FILTER (
        FactProductionData,
        FactProductionData[Date] <= LASTDATE ( FactProductionData[Date] )
            && FactProductionData[Date]
                >= DATEADD(FactProductionData[Date], -12,DAY)
    )
)

 

 

Hi @arifulice

 

Another way

 

=
CALCULATE (
    AVERAGE ( FactProductionData[Egg Prod#Qty] ),
    DATESINPERIOD (
        FactProductionData[Date],
        MAX ( FactProductionData[Date] ) - 11,
        12,
        DAY
    )
)

Regards
Zubair

Please try my custom visuals

Hi @Zubair_Muhammad

 

 

 

 

 

After applying your DAX ,when i dropped the measure in pivot table the following error message is showing attached in photo

EggProduction:=CALCULATE (
    AVERAGE ( FactProductionData[Egg Prod#Qty] ),
    DATESINPERIOD (
        FactProductionData[Date],
        MAX ( FactProductionData[Date] ) - 11,
        12,
        DAY
    )
)

Capture.PNG

 

Hi @arifulice

 

This formula works fine with me. Smiley Surprised

 

see the attached file

 

 


Regards
Zubair

Please try my custom visuals

Hi @Zubair_Muhammad

 

I want to see the value of last 12 days but the below link giving the result of all month.please see the attached helowCapt.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.