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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
RSip
Frequent Visitor

Repeating values based on condition in DAX

Greetings,

 

I have this problem which I am unable to solve by myself:

 

I have a table which holds the information of different test subjects, the data is grouped, ranked and sorted. The column "strength35" is used to calculate columns "MIN", "MAX", "REDLINE", then columns "strength15" and "AVERAGE" are used to compare with these values. Rows are ranked by date and group, but for the sake of simplicity only a single group is taken into account at the moment.

I am trying to create a column in DAX, that would keep returning the first value from the column "strength35" until the value in column "strength15" is lower than "MIN" or higher than "MAX" or the value in column "AVERAGE" is lower than "REDLINE", then a new value from "strength35" should be picked, which would need to be returned every row until again, the condition is met and a new value must be picked. First 34 rows of information needed to be skipped but not filtered away that is why I used "IF" function in such a way:

NEWCOLUMN =
VAR str35 = table[strength35]
VAR str15 = table[strength15]
VAR MIN = esama_deviacija * 0.63
VAR MAX = esama_deviacija * 1.37
VAR REDLINE = 40 + 1.48 * esama_deviacija
VAR TEST =
IF(table[Rank(desc)]<35,BLANK(),
IF(table[Rank(desc)]=35,str35,
IF(MIN<=str15 && MAX>=str15 && REDLINE<=table[AVERAGE],strength35,str35)))
RETURN TEST

 

When writting this I somehow thought that "strength35" would be calculated for each row and str35 would be calculated only when the condition is no longer met, but I was completely wrong and I am unsure how to fix it.

 

The table i got:

 

DATERANKstrength35MINstrength15MAXREDLINEAVERAGENEWCOLUMN
2023-06-11445,713,603,727,8348,4553,185,71
2023-06-10435,733,613,687,8548,4853,185,73
2023-06-094211,557,287,9615,8357,1053,185,95
2023-06-08416,313,974,918,6449,3353,186,31
2023-06-07406,564,135,028,9849,7153,186,56
2023-05-21395,313,354,807,2847,8653,185,31
2023-05-20384,853,064,966,6547,1853,184,85
2023-05-19374,943,115,476,7747,3253,184,94
2023-05-10368,255,206,1911,3152,2153,188,25
2023-05-09358,355,266,0011,4452,3653,188,35

 

 

The  table I was trying to get:

DATERANKstrength35MINstrength15MAXREDLINEAVERAGENEWCOLUMN
2023-06-11445,713,603,727,8348,4553,185,73
2023-06-10435,733,613,687,8548,4853,185,73
2023-06-094211,557,287,9615,8357,1053,1811,55
2023-06-08416,313,974,918,6449,3353,184,85
2023-06-07406,564,135,028,9849,7153,184,85
2023-05-21395,313,354,807,2847,8653,184,85
2023-05-20384,853,064,966,6547,1853,184,85
2023-05-19374,943,115,476,7747,3253,188,35
2023-05-10368,255,206,1911,3152,2153,188,35
2023-05-09358,355,266,0011,4452,3653,188,35

 

For some reason altering table options gives errors and prevents me from posting, so I boldened characters every second column for clarity.

If anyone has any suggestions or solutions, I would be very grateful.

7 REPLIES 7
gmsamborn
Super User
Super User

Hi @RSip 

 

I took another look at [NEWCOLUMN] and realized that it will ALWAYS equal 'Table'[strength35].

 

In the first line, you set str35 to 'Table'[strength35] meaning that the variable won't change.  Also, there is nothing in your DAX that would change the context, that would change the returned value for 'Table'[strength35] in any way.

 

 

NEWCOLUMN = 
VAR _str35 = 'Table'[strength35]
VAR _str15 = 'Table'[strength15]
VAR _MINIMUM = _str35 * 0.63
VAR _MAXIMUM = _str35 * 1.37
VAR _REDLINE = 40 + ( 1.48 * _str35 )
VAR _TEST =
    IF (
        'Table'[RANK] < 35,
        BLANK (),
        IF (
            'Table'[RANK] = 35,
            'Table'[strength35],
            IF (
                _MINIMUM <= _str15
                    && _MAXIMUM >= _str15
                    && _REDLINE <= 'Table'[AVERAGE],
                _str35,
                'Table'[strength35]
            )
        )
    )
RETURN
    _str35

 

 

Let me know if you have any questions.

v-nuoc-msft
Community Support
Community Support

Hi @RSip 

 

I tested your problem and found some issues.

 

For the result you want, "str15" matches the condition you set in dax, i.e. a data rank of 38.

 

According to your description, NEWCOLUMN should output "835" to be logical.

 

However, you want it to output its own value "str35".

 

This is what I don't understand. Do you have another judgment condition present?

 

It would be best to provide the pbix file if you can and be careful to remove sensitive data.

 

This will allow me to help you in more detail.

 

Regards,

Nono Chen

 

Greetings,

 

I have made a dummy table in PowerBi and created "NEWCOLUMN". I have attached a link to pbix file in my dropbox. I hope this helps to answer my question. Thank you in advance.

Link to dummy table in dropbox 

I apologise for the late reply.  I noticed that the formula I attached was not the one I used in my PowerBi, this is what I used:

VAR str35 = table[strength35]
VAR str15 = table[strength15]
VAR MINIMUM = str35 * 0.63
VAR MAXIMUM = str35 * 1.37
VAR REDLINE = 40 + 1.48 * str35
VAR TEST =
    IF (
        table[Rank(desc)] < 35,
        BLANK (),
        IF (
            table[Rank(desc)] = 35,
            table[strength35],
            IF (
                MIN <= str15
                    && MAX >= str15
                    && REDLINE <= table[AVERAGE],
                str35,
                table[strength35]
            )
        )
    )
RETURN
    TEST

 

In a row of rank 38, while 'strength35' and 'strength15' almost match, MIN value from row ranked 35 is 5.26 which is higher than 4.96 'strength15' value in row ranked 38. What I am trying to achieve in this table is to find a value, calculate minimum, maximum and redline boundries for it and use these values until the boundries are crossed, then get a new value and calculate its boundries and so on. 

 

I do not have another judgement condition present.

 

In a day or two I should be able to create a pbix file with dummy data.

 

 

 

gmsamborn
Super User
Super User

Hi @RSip 

 

I imported your data and tried to create your NEWCOLUMN.

 

I noticed that you were using MIN and MAX as variable names.  This isn't allowed because they are reserved keywords.

 

I'm stuck on determining what 'esama_deviacija' is.  Because it isn't enclosed in [ and ], rules out it being a measure or column.  The only things I can think of are a) a variable name you haven't defined or b) a table in which case you would have to refer to a column.

 

Can you explain 'esama_deviacija'?

Hello,

 

I have created a dummy table with "NEWCOLUMN". I'm attaching the link to pbix file in dropbox below:

Dummy table in dropbox .I hope this will be of some help. Thank you in advance.

I apologise for late reply. 'esama_deviacija' is a variable name that I used in my PowerBi file, that I mistakenly did not rename for this example. 'esama_deviacija' should have been named str35. This should work:

 

NEWCOLUMN =
VAR str35 = table[strength35]
VAR str15 = table[strength15]
VAR MINIMUM = str35 * 0.63
VAR MAXIMUM = str35 * 1.37
VAR REDLINE = 40 + 1.48 * str35
VAR TEST =
    IF (
        table[Rank(desc)] < 35,
        BLANK (),
        IF (
            table[Rank(desc)] = 35,
            table[strength35],
            IF (
                MIN <= str15
                    && MAX >= str15
                    && REDLINE <= table[AVERAGE],
                str35,
                table[strength35]
            )
        )
    )
RETURN
    TEST

 

Hopefuly this will clear up things.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors