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

DAX for Loop and write mechanism in a calculated column

Hello , 

 

I have some sample data shown as below.

I've been struggling to write DAX for the column Flag_2 which is sort of a cursor / loop that iterates over Month EndDate , for every unique ID. The objective of Flag_2 is to meet the first occurence of "1" in the column Flag_1 whilst looping ascending over Month Enddate, and marking the rest as 'x'. or -1, whatever works. 

I'm pretty sure with collecting the date ranges, and writing calculate aggregations on them but i'm lost with not having the ability to implement loops and local variables or flags. How does one solve this problem in DAX ?

mjsushanth_0-1659608557284.png

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous,

The formula expressions logic:

1. Get the first date based on 'flag_1' field value and id.

2. Use the 'first date' to compare with current date and check 'flag_1' field value to remark tags.

formula =
VAR currDate =
    DATEVALUE ( "1-" & Table[Month Name] )
VAR _firstDate =
    CALCULATE (
        MIN ( Table[Month Name] ),
        FILTER (
            Table,
            [ID] = EARLIER ( Table[ID] )
                && [Flag_1] = 1
                && DATEVALUE ( "1-" & Table[Month Name] )
                    < DATEVALUE ( "1-" & EARLIER ( Table[Month Name] ) )
        )
    )
RETURN
    IF (
        Table[Flag_1] = 1
            && currDate
                = DATEVALUE ( "1-" & _firstDate ),
        "1",
        IF ( currDate > DATEVALUE ( "1-" & _firstDate ), "x", "0" )
    )

Regards,

Moonlight

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

HI @Anonymous,

It seems like you check the first flag1 based on id and month name fields.

I'd like to suggest you add an index column in the query editor based on these two fields, then you can loop records based on the current id and index to remark the first '1' on flag_2 filed.

Regards,

Moonlight

Anonymous
Not applicable

Hello , 
Thank you for that suggestion ! but i have flag_1 as a calculated column in my model that's actually a result of many other logical operations, conditions. I also do this on different levels, an ID , month_name , person name fields ... etc. 

But feel free to share how you would loop records after making an index column/concatenated column and flag the first occurence as 1, and the rest as 'x'. 

Anonymous
Not applicable

Hi @Anonymous,

The formula expressions logic:

1. Get the first date based on 'flag_1' field value and id.

2. Use the 'first date' to compare with current date and check 'flag_1' field value to remark tags.

formula =
VAR currDate =
    DATEVALUE ( "1-" & Table[Month Name] )
VAR _firstDate =
    CALCULATE (
        MIN ( Table[Month Name] ),
        FILTER (
            Table,
            [ID] = EARLIER ( Table[ID] )
                && [Flag_1] = 1
                && DATEVALUE ( "1-" & Table[Month Name] )
                    < DATEVALUE ( "1-" & EARLIER ( Table[Month Name] ) )
        )
    )
RETURN
    IF (
        Table[Flag_1] = 1
            && currDate
                = DATEVALUE ( "1-" & _firstDate ),
        "1",
        IF ( currDate > DATEVALUE ( "1-" & _firstDate ), "x", "0" )
    )

Regards,

Moonlight

Anonymous
Not applicable

Thank you for this , 
I've used some workarounds with summations, distinct counts and such logics, but i'm going to experiment more with your solution. 🙂 

Hi @Anonymous,

Did the above suggestions help with your scenario? if that is the case, you can consider Kudo or Accept the helpful suggestions to help others who faced similar requirements.

If these also don't help, please share more detailed information to help us clarify your scenario to test.

How to Get Your Question Answered Quickly 

Regards,

Xiaoxin Sheng

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

@Anonymous ,

a new column =

var _date = minx(filter(Table, [id] =earlier([ID]) ), [Date])

return

Switch(True(),

[Date] < _date, "0",

[Date] =  _date, "1",

"x"

)

Anonymous
Not applicable

I do not see the new column's DAX using the column Flag_1 at all. 

This answer doesn't work because it dummifies the problem- bearing the first month's ( min month ) value to be 1 and the rest turn 'x'. It doesnt consider Flag_1.

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.