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
Anonymous
Not applicable

For loop in power bi

Hello!

 

I created a measure that calculate a score based on several variables. When I filter the table by the current week, I would need to build 2 columns:

1. One column including "yes" or "no" if this score was below 50 in at least one of the 5 previous weeks.

2. One column including how many weeks (of the previous 5 weeks) in which this score was below 50.

 

In python, I think this could be done with a foor loop but I dont know how could I get that result in Dax. Any help? Thank you so much!!

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

I created some data:

vyangliumsft_0-1650419951336.png

Here are the steps you can follow:

1. Create calculated table.

Table = DISTINCT('Fortable'[date])

vyangliumsft_1-1650419951338.png

2. Create measure.

Flag =
var _select=SELECTEDVALUE('Table'[date])
return
IF(
    WEEKNUM(MAX('Fortable'[date]),1)<WEEKNUM(_select,1)&&WEEKNUM(MAX('Fortable'[date]),1)>=WEEKNUM(_select,1) -7 ,1,0)
Sum =
var _select=SELECTEDVALUE('Table'[date])
var _sum=CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week])))
return
_sum
weeks_measure =
var _select=SELECTEDVALUE('Table'[date])
var _sum=CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week])))
return
CALCULATE(DISTINCTCOUNT('Fortable'[week]),FILTER( ALLSELECTED('Fortable'),[Sum]>50))
Measure =
var _select=SELECTEDVALUE('Table'[date])
var _sum=CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week])))
var _if=if(
_sum>50,"Yes","No")
var _table=SUMMARIZE(ALLSELECTED(Fortable),'Fortable'[date],"1",CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week]))))
return
IF(
COUNTROWS(FILTER(_table,[1]>=50))>1,"Yes","no")

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_2-1650419951339.png

4. Result:

vyangliumsft_3-1650419951343.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

Do you have a Date column.  If yes, then build a Calendar Table and ensure that there is a Week number column in the Calendar Table.  After you have done this, share the download link of the PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

I created some data:

vyangliumsft_0-1650419951336.png

Here are the steps you can follow:

1. Create calculated table.

Table = DISTINCT('Fortable'[date])

vyangliumsft_1-1650419951338.png

2. Create measure.

Flag =
var _select=SELECTEDVALUE('Table'[date])
return
IF(
    WEEKNUM(MAX('Fortable'[date]),1)<WEEKNUM(_select,1)&&WEEKNUM(MAX('Fortable'[date]),1)>=WEEKNUM(_select,1) -7 ,1,0)
Sum =
var _select=SELECTEDVALUE('Table'[date])
var _sum=CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week])))
return
_sum
weeks_measure =
var _select=SELECTEDVALUE('Table'[date])
var _sum=CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week])))
return
CALCULATE(DISTINCTCOUNT('Fortable'[week]),FILTER( ALLSELECTED('Fortable'),[Sum]>50))
Measure =
var _select=SELECTEDVALUE('Table'[date])
var _sum=CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week])))
var _if=if(
_sum>50,"Yes","No")
var _table=SUMMARIZE(ALLSELECTED(Fortable),'Fortable'[date],"1",CALCULATE(SUM('Fortable'[score]),FILTER(ALLSELECTED(Fortable),WEEKNUM('Fortable'[date],1)<=WEEKNUM(_select,1)&&WEEKNUM('Fortable'[date],1)>=WEEKNUM(_select,1) -7&&'Fortable'[week]=MAX('Fortable'[week]))))
return
IF(
COUNTROWS(FILTER(_table,[1]>=50))>1,"Yes","no")

3. Place [Flag]in Filters, set is=1, apply filter.

vyangliumsft_2-1650419951339.png

4. Result:

vyangliumsft_3-1650419951343.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

amitchandak
Super User
Super User

@Anonymous , You can do that with help from earlier

example

countx(filter(Table, Table[Week] >= earlier([Week]) -5  && Table[Week] <= earlier([Week])  && [Score] <50) , [Score])

 

refer: https://www.goodly.co.in/earlier-function-dax-power-bi/

 

If this does not help
Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

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.