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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Simplify the code

Hey, 

 

Would you have an idea to simplify my dax calcul ? 

 

Sniv_0-1655124177949.png

 

I want to do this for a month, so it's more complicated.

6 REPLIES 6
Greg_Deckler
Community Champion
Community Champion

@Anonymous Perhaps use MONTH and YEAR to get the month and year of your [Date] and then use a CALCULATE(SUM where the month and year of the [Date] equals those values? Hard to determine what exactly you are trying to do.

 

Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi, thanks for your answer.

 

The fact is that i need to do a weighted average on a month, (like what i did for the week). As you can see the second variable [Nombre de sessions cumulées par semaine] isn't affected by the date (dateadd). 

 

To simplify i want to do exactely the same thing you can see on the screen with only function if it does exist. 

@Anonymous Again, not following. First, I don't see any variables (VAR statements) in your code. And where the weighted average comes into play is not clear. I would have never guessed that was a weighted average formula. Without example data and expected output, it's hard to tell you what you need to do.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler 

I'll give you an example of what i actually want on excel. 

There is the context : 

Sniv_0-1655127082978.png

I want to calculate the cumulative average time by session for the day 5 for example. 

 

So I start by calculating each day, depending on the number of session, the average time by session and the cumulative session which is the sum of all the sessions. 

Sniv_1-1655127208552.png

Then to have my result i need to sum all of them, 

 

Sniv_2-1655127242264.png

I want to do the same on power bi on a month period for each day. 

Example : if i choose the 1st February, i want the cumulative average time by session from 1st January to 1st February. 

 

Sorry if i'm not clear enough, my english isn't the best. 

 

Thanks for your answers. 

@Anonymous So typically I would do something like that this way:

Measure = 
  VAR __Date = MAX(Dates[Date])
  VAR __MonthAgo = DATE(YEAR(EOMONTH(__Date,-1)),MONTH(EOMONTH(__Date,-1)),DAY(__Date))
  VAR __Table = ADDCOLUMNS(FILTER(ALL('Dates'),[Date]>=__MonthAgo && [Date]<=__Date),"__MyCalc",[your daily calculation goes here])
RETURN
  SUMX(__Table,[__MyCalc])

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Thanks, 

 

But what i ask for is to be able to use the cumulative session value of the day 5 for the calcul of all the other day. It need to be the same because it depend of the last day while the number of session and the average time is different each time. Let me know if i'm not clear enough. 

 

 

 

 

Sniv_0-1655134342530.png

Sniv_2-1655134462325.png

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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