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
prat_daxer
New Member

Assigning a column value based on other column

I have data something like this:

Catagoryvaluefinal value
A11
A21
B22
B32
B42

Basically number 1,2,3,4 are priority and if any atagory has prio 1 also 2 assign both to 1.

2 ACCEPTED SOLUTIONS
pi_eye
Resolver IV
Resolver IV

It sounds like you are just trying to get the min of each group /category is that right?

You'll need an expression like

GroupMin = calculate(min('Table'[Value]), ALLEXCEPT('Table','Table'[Cat]))
Calculate will give you the min, the first parameter calculation, and the context is specified in the second paramter, AllExcept. In this case the context "all except" tells DAX to return all values in the table, ie, ignore all groupings per row, except the cat grouping.
 
This gives:
pi_eye_0-1665042673675.png

 

Pi

View solution in original post

The solution then is something like this:

Extended Logic =
var contains5= calculate(CONTAINS('Table','Table'[Value],5),ALLEXCEPT('Table','Table'[Category]))
var contains6=calculate(CONTAINS('Table','Table'[Value],6),ALLEXCEPT('Table','Table'[Category]))
return Switch (FIRSTNONBLANK( 'Table'[Category],0) ,
    "A",if(contains6,FIRSTNONBLANK('Table'[Value],0),5),
    "B",if(contains5,FIRSTNONBLANK('Table'[Value],0),6),
    "C",if(and(contains5,contains6),floor(5+rand()+0.25,1)))
 
vars contains5 and contains6 = these evaluate within the context of category, if the values 5 and 6 exist, respectively.

Switch Is similar to a case statement if you are familar with those - if you are not familiar think of it as an extended IF() statement where you can return multiple values depending on an input value.
 
Here we are passing the category to the switch statement, and depending on which category it is, returning a different value.
IF category is A, we look use contains 6 to determine what to return - either the value, or 5. Similar to B.
for C we are checking both contains5 and contains6 and if that's the case, using rand() to assign 5's and 6's
 
HTH 
Pi

View solution in original post

6 REPLIES 6
prat_daxer
New Member

 

Catagoryvaluefinal value
A55
A75
B76
B66
C55
C65
C55
C66

Condition 1: Cat A --> Assign prio 5 only if 6 is not there against A.

Condition 2: Cat B --> Assign prio 6 only if 5 is not present against B.

Condition 3: Cat C --> If prio 5 and 6 both present against C, assign random 75% 5's to cat C and in rest 25%, 6's.

There's as extended query to above problem. @pi_eye 

The solution then is something like this:

Extended Logic =
var contains5= calculate(CONTAINS('Table','Table'[Value],5),ALLEXCEPT('Table','Table'[Category]))
var contains6=calculate(CONTAINS('Table','Table'[Value],6),ALLEXCEPT('Table','Table'[Category]))
return Switch (FIRSTNONBLANK( 'Table'[Category],0) ,
    "A",if(contains6,FIRSTNONBLANK('Table'[Value],0),5),
    "B",if(contains5,FIRSTNONBLANK('Table'[Value],0),6),
    "C",if(and(contains5,contains6),floor(5+rand()+0.25,1)))
 
vars contains5 and contains6 = these evaluate within the context of category, if the values 5 and 6 exist, respectively.

Switch Is similar to a case statement if you are familar with those - if you are not familiar think of it as an extended IF() statement where you can return multiple values depending on an input value.
 
Here we are passing the category to the switch statement, and depending on which category it is, returning a different value.
IF category is A, we look use contains 6 to determine what to return - either the value, or 5. Similar to B.
for C we are checking both contains5 and contains6 and if that's the case, using rand() to assign 5's and 6's
 
HTH 
Pi
pi_eye
Resolver IV
Resolver IV

It sounds like you are just trying to get the min of each group /category is that right?

You'll need an expression like

GroupMin = calculate(min('Table'[Value]), ALLEXCEPT('Table','Table'[Cat]))
Calculate will give you the min, the first parameter calculation, and the context is specified in the second paramter, AllExcept. In this case the context "all except" tells DAX to return all values in the table, ie, ignore all groupings per row, except the cat grouping.
 
This gives:
pi_eye_0-1665042673675.png

 

Pi

prat_daxer
New Member

DAX would be good for me, I dont want to go on Mquery for this

pi_eye
Resolver IV
Resolver IV

Hi prat_daxer,

 

Are you trying to do this with DAX (front end UI) or M query (data shaping and modelling)

 

Pi

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.