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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
arman_tale
New Member

IF combination with LOOKUP

Hi all,

 

Let's assume I have the following table

 

Column1Column2
1B
1A
2A
2B
3A
4B
5B
6C
6B
7C

 

I need to create a c custom column with the priority of and if NOT, find B, if NOT show null.

Examplle:

Column1Column2CustomColumn
1BA
1AA
2AA
2BA
3AA
4BB
5BB
6CB
6BB
7Cnull

 

 

Highly appreciate your support!

1 ACCEPTED SOLUTION

Hi @arman_tale - check the below calculated column and change the table name as per your source.

Custom1 =
VAR CurrentValue = 'Vlook'[Column2]
VAR PriorityA = CALCULATE(MAX('Vlook'[Column2]), 'Vlook'[Column1] = EARLIER('Vlook'[Column1]) && 'Vlook'[Column2] IN {"A", "B"})
VAR PriorityC = CALCULATE(MAX('Vlook'[Column2]), 'Vlook'[Column1] = EARLIER('Vlook'[Column1]) && 'Vlook'[Column2] IN {"C", "D"})
VAR PriorityE = CALCULATE(MAX('Vlook'[Column2]), 'Vlook'[Column1] = EARLIER('Vlook'[Column1]) && 'Vlook'[Column2] IN {"E", "F"})

RETURN
    IF(CurrentValue IN {"A", "B"},
        PriorityA,
    IF(CurrentValue IN {"C", "D"},
        PriorityC,
    IF(CurrentValue IN {"E", "F"},
        PriorityE,
    BLANK())))

rajendraongole1_1-1723539918948.png

 

Hope it works

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

9 REPLIES 9
ryan_mayu
Super User
Super User

@arman_tale 

you can try this

 
Column =
VAR _p=CALCULATE(min('Table'[Column2]),ALLEXCEPT('Table','Table'[Column1]))
return if (_p="A", "A",if(_p="B","B",blank()))
 
11.PNG




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




arman_tale
New Member

@rajendraongole1 @SamWiseOwl 
That works perfect, thank you. But I actually realized the problem I am facing is a bit deeper than that:

Let's say Column1 is independant variable, but I have dependant variables in groups of 2 (A and B), (C and D), (E and F). A has priority to B, C has priority to D and E has priority to F, and I need to show all the independant Column1 variables with the highest available priority variable in each group

I need my customcolumn to look like this

 

Column1Column2Custom
1BA
1AA
1CC
1DC
2AA
2DC
2CC
3FF
4EE
4FE
4BB
5AA
5BA
5CC
5DC
5EE
5FE

 

Appreciate your feedback and support!

Hi @arman_tale - Create a new custom column in power query editor as below

Custom =

if [Column2] = "A" or [Column2] = "B" then "A"
else if [Column2] = "C" or [Column2] = "D" then "C"
else if [Column2] = "E" or [Column2] = "F" then "E"
else null

 

rajendraongole1_1-1723532923072.png

 

output:

rajendraongole1_2-1723532942310.png

 

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





@rajendraongole1 

This doesn't preceisely work the way I required. Look at the issue in the screenshot:
2024-08-13_10-04-49.jpg

 

Any approach to overcome this issue?

Hi @arman_tale - check the below calculated column and change the table name as per your source.

Custom1 =
VAR CurrentValue = 'Vlook'[Column2]
VAR PriorityA = CALCULATE(MAX('Vlook'[Column2]), 'Vlook'[Column1] = EARLIER('Vlook'[Column1]) && 'Vlook'[Column2] IN {"A", "B"})
VAR PriorityC = CALCULATE(MAX('Vlook'[Column2]), 'Vlook'[Column1] = EARLIER('Vlook'[Column1]) && 'Vlook'[Column2] IN {"C", "D"})
VAR PriorityE = CALCULATE(MAX('Vlook'[Column2]), 'Vlook'[Column1] = EARLIER('Vlook'[Column1]) && 'Vlook'[Column2] IN {"E", "F"})

RETURN
    IF(CurrentValue IN {"A", "B"},
        PriorityA,
    IF(CurrentValue IN {"C", "D"},
        PriorityC,
    IF(CurrentValue IN {"E", "F"},
        PriorityE,
    BLANK())))

rajendraongole1_1-1723539918948.png

 

Hope it works

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Love an example with Earlier!
Do you have any advice regarding when Earlier is better than storing (in this case) Column1 in its own variable?


If you are happy with this answer please mark as a solution for others to find !

Kudos are always appreciated! Check out our free Power BI video courses.

Thank you @SamWiseOwl , yes when you need to perform outer row context manipulation,while you are inside a nested row context in particularly when you're working with nested row contexts in the above scenerios, it work fine. 

 

@arman_tale - please check 

Thank you





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





rajendraongole1
Super User
Super User

Hi @arman_tale -you can do group by Column1, and for the Column2 column, create an "All Rows" operation (this will create a table for each Column1).

rajendraongole1_0-1723467996397.png

 

add one new column (Custom column) with below if condition as :

if Table.Contains([AllRow], [Column2 = "A"]) then "A"
else if Table.Contains([AllRow], [Column2 = "B"]) then "B"
else null

 

rajendraongole1_1-1723468043133.png

 

Expand Allrows column2

rajendraongole1_2-1723468074327.png

 

output:

rajendraongole1_3-1723468091522.png

 

 

Hope it helps





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





SamWiseOwl
Super User
Super User

Hi @arman_tale 
You could do this in the front end with a Calculated Column :

Custom Column =
 var CurrentGroup = [Column1] --Capture group
 var SameGroup = --Filter to same group
 SELECTCOLUMNS(
    Filter('Table', [Column1] = CurrentGroup)
    ,[Column2]) --return single column
RETURN
 SWITCH(
    TRUE()
    ,"A" IN SameGroup
     ,"A"
        ,"B" IN SameGroup
     ,"B"
     ,"null"
 )
SamWiseOwl_0-1723468010464.png

 


If you are happy with this answer please mark as a solution for others to find !

Kudos are always appreciated! Check out our free Power BI video courses.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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