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
MathLacome
Regular Visitor

Calculated Column & Référence to one line.

Hi everyone, 

 

I'm a fresh user of Power BI (less than 6 months experience) but OK in Excel.

 

I have some dashboard that I would like to move from Excel to Power BI but still struggling with some calculated rows.

 

In Excel, I have a moving mean  (or sum) calculated for each different people with basics formula like : 

 

SUMIFS((Total Distance]; [Date]; "<="[@Date];[Date];">"&[@Date]-7;[Player];[@Player])

 

I switched in Power Bi with a simple : 

 

Calculate(sum([Total Distance)]; Datesinperiod (...) ; 

 

But I don't know what to add to replace my player filter [@Player]...

 

Anyone could help me ?

 

All the best,

 

Mathieu.

1 ACCEPTED SOLUTION
BhaveshPatel
Community Champion
Community Champion

 

First Requirement:

You should create a calender table in your data model and create a relationship between your date table and facttable(Transactions Table).

 

To Create Total 12 months moving average Distance Measure, You should create the below measures.

 

TotalDistance:=SUM(FactTable[Total Distance])

 

TotalDistance12Mon := CALCULATE (
    [TotalDistance],
    DATESBETWEEN (
        Calendar[Date],
        NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( Calendar[Date] ) ) ),
        LASTDATE ( Calendar[Date] )
    )
)
 
Create a third measure to count the number of periods in a transactions table
 
Months12 := CALCULATE (
    CALCULATE ( COUNTROWS ( VALUES ( Calendar[MonthName] ) ), FactTable),
    DATESBETWEEN (
        Calendar[Date],
        NEXTDAY ( SAMEPERIODLASTYEAR (LASTDATE ( Calendar[Date] ) ) ),
        LASTDATE ( Calendar[Date] )
    )
)
 
 
For moving average of 12 months:
Create a fourth measure as shown below.
 
MovingAverage12months:= DIVIDE ( [TotalDistance], [TotalDistance] ) * DIVIDE ( [TotalDistance12Mon], [Months12] )
 
 
Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

View solution in original post

3 REPLIES 3
v-shex-msft
Community Support
Community Support

Hi @MathLacome,

 

Since I'm not very clear with your variables [@Date] and [@Player], can you share me some detail information about these?

 

I try to convert your formula to dax, perhaps you can try to use it.

 

if the @ variables means current value:

Total=
var currDate=MAX([Date])
var currPlayer=LASTNONBLANK(Table[Player],[Player])
return
SUMX(FILTER(ALL(Table),AND([Date]<=currDate,[Date]>currDate-7)&&[Player]=currPlayer),[Price])

 

if the @ variables means selected value:

 

Measures:

SelectDate=if(HASONEVALUE(Table[Date]),VALUES(Table[Date]),BLANK())

SelectPlayer=if(HASONEVALUE(Table[Player]),VALUES(Table[Player]),BLANK())

 

Total=
SUMX(FILTER(ALL(Table),AND([Date]<=[SelectDate],[Date]>[SelectDate]-7)&&[Player]=[SelectPlayer]),[Price])

 

If above is not help, please share us more detail information.

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
BhaveshPatel
Community Champion
Community Champion

 

First Requirement:

You should create a calender table in your data model and create a relationship between your date table and facttable(Transactions Table).

 

To Create Total 12 months moving average Distance Measure, You should create the below measures.

 

TotalDistance:=SUM(FactTable[Total Distance])

 

TotalDistance12Mon := CALCULATE (
    [TotalDistance],
    DATESBETWEEN (
        Calendar[Date],
        NEXTDAY ( SAMEPERIODLASTYEAR ( LASTDATE ( Calendar[Date] ) ) ),
        LASTDATE ( Calendar[Date] )
    )
)
 
Create a third measure to count the number of periods in a transactions table
 
Months12 := CALCULATE (
    CALCULATE ( COUNTROWS ( VALUES ( Calendar[MonthName] ) ), FactTable),
    DATESBETWEEN (
        Calendar[Date],
        NEXTDAY ( SAMEPERIODLASTYEAR (LASTDATE ( Calendar[Date] ) ) ),
        LASTDATE ( Calendar[Date] )
    )
)
 
 
For moving average of 12 months:
Create a fourth measure as shown below.
 
MovingAverage12months:= DIVIDE ( [TotalDistance], [TotalDistance] ) * DIVIDE ( [TotalDistance12Mon], [Months12] )
 
 
Thanks & Regards,
Bhavesh

Love the Self Service BI.
Please use the 'Mark as answer' link to mark a post that answers your question. If you find a reply helpful, please remember to give Kudos.

@MathLacome I'm not an Excel guy, but if I read the function correctly you are basically adjusting the formula for each person... If that's the case, then you can just create the measure without that part. In Power BI, you would use a slicer or filter on the report page to adjust the calculation. Otherwise, if the measure is used along with the person in the visual, the measure will automatically aggregate with any related field.


Looking for more Power BI tips, tricks & tools? Check out PowerBI.tips the site I co-own with Mike Carlo. Also, if you are near SE WI? Join our PUG Milwaukee Brew City PUG

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.