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

View all the Fabric Data Days sessions on demand. View schedule

Reply
Anonymous
Not applicable

Create a new column based on multiple columns and grouping

Hi,

 

I need to create a new column based on family members under 1 employee code.  Below is the data that I have and column that I need to create ie Family Definition

 

amitdarak_1-1653994585619.png

 

As can be seen from above data, if 1 employee code has
- Self then E,
- Self, Spouse, Children, Parent then ESCP.
- Self, Spouse then ES

- Self, Parent then SP and so on.

 

Also taking year into consideration is also important as previous year combination may be different as is the case for empl code 123.

 

How can this be achieved?

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below Calculated Column DAX formula and the attached pbix file.

It is for creating a calculated column.

 

Family definition CC =
VAR _conditiontable =
    SUMMARIZE (
        FILTER (
            Data,
            Data[Year] = EARLIER ( Data[Year] )
                && Data[Employee code] = EARLIER ( Data[Employee code] )
        ),
        Data[Relation]
    )
RETURN
    SWITCH (
        TRUE (),
        { "Self" }
            IN _conditiontable
            && { "Spouse" }
            IN _conditiontable
            && { "Children" }
            IN _conditiontable
            && { "Parent" } IN _conditiontable, "ESCP",
        { "Self" }
            IN _conditiontable
            && { "Spouse" }
            IN _conditiontable
            && { "Children" } IN _conditiontable, "ESC",
        { "Self" }
            IN _conditiontable
            && { "Spouse" } IN _conditiontable, "ES",
        { "Self" }
            IN _conditiontable
            && { "Parent" } IN _conditiontable, "EP",
        { "Self" } IN _conditiontable, "E",
        "Check the logic"
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

4 REPLIES 4
vojtechsima
Super User
Super User

Hi, @Anonymous 
I am also adding a different version as a measure:

 

 

 

 

Familiy definiton___ = 
var CurrentCode = SELECTEDVALUE ( FamilyDefinitionTable[Employee code] ) 
var CurrentYear = SELECTEDVALUE (FamilyDefinitionTable[Year] )
var theTable = CALCULATETABLE(SUMMARIZE(FamilyDefinitionTable, "Year", CurrentYear , "Emp_Code",CurrentCode, "AllRelations", CALCULATE(CONCATENATEX(FamilyDefinitionTable, FamilyDefinitionTable[Relation], ","))), ALLEXCEPT(FamilyDefinitionTable, FamilyDefinitionTable[Year],FamilyDefinitionTable[Employee code]))
var ADDRelations = 
ADDCOLUMNS(theTable, "Family Definition", 

    var CheckSelf = IF(CONTAINSSTRING([AllRelations], "Self"), "E")
    var CheckSpouse = IF(CONTAINSSTRING([AllRelations], "Spouse"), "S")
    var CheckChildren = IF(CONTAINSSTRING([AllRelations], "Children"), "C")
    var CheckParent = IF(CONTAINSSTRING([AllRelations], "Parent"), "P")
    var Together = CheckSelf&CheckSpouse&CheckChildren&CheckParent
return Together
)


var FinxTheCode = MAXX(FILTER(ADDRelations, [Year] = CurrentYear && [Emp_Code] = CurrentCode), [Family Definition])

return FinxTheCode

 

 

 

 

The check for the letters in this one is probably a bit cleaner.

vojtechsima_0-1653999330721.png

As calculated column here:

FamilyColumn = 
var CurrentCode =  FamilyDefinitionTable[Employee code] 
var CurrentYear = FamilyDefinitionTable[Year] 
var theTable = CALCULATETABLE(SUMMARIZE(FamilyDefinitionTable, "Year", CurrentYear , "Emp_Code",CurrentCode, "AllRelations", CALCULATE(CONCATENATEX(FamilyDefinitionTable, FamilyDefinitionTable[Relation], ","))), ALLEXCEPT(FamilyDefinitionTable, FamilyDefinitionTable[Year],FamilyDefinitionTable[Employee code]))
var ADDRelations = 
ADDCOLUMNS(theTable, "Family Definition", 

    var CheckSelf = IF(CONTAINSSTRING([AllRelations], "Self"), "E")
    var CheckSpouse = IF(CONTAINSSTRING([AllRelations], "Spouse"), "S")
    var CheckChildren = IF(CONTAINSSTRING([AllRelations], "Children"), "C")
    var CheckParent = IF(CONTAINSSTRING([AllRelations], "Parent"), "P")
    var Together = CheckSelf&CheckSpouse&CheckChildren&CheckParent
return Together
)


var FinxTheCode = MAXX(FILTER(ADDRelations, [Year] = CurrentYear && [Emp_Code] = CurrentCode), [Family Definition])

return FinxTheCode
Tahreem24
Super User
Super User

@Anonymous Try this measure:

Measure 2 =
VAR a_ = CALCULATE(COUNTROWS(FamilyTable),ALLEXCEPT(FamilyTable,FamilyTable[Emp Code],FamilyTable[Year]))
VAR EP_ = CALCULATE(COUNTROWS(FamilyTable),FamilyTable[Relation] in {"Self","Parent"},ALLEXCEPT(FamilyTable,FamilyTable[Year],FamilyTable[Emp Code]))
VAR SP_ = CALCULATE(COUNTROWS(FamilyTable),FamilyTable[Relation] in {"Self","Spouse"},ALLEXCEPT(FamilyTable,FamilyTable[Year],FamilyTable[Emp Code]))
VAR ESC_ = CALCULATE(COUNTROWS(FamilyTable),FamilyTable[Relation] in {"Self","Spouse","Chidren"},ALLEXCEPT(FamilyTable,FamilyTable[Year],FamilyTable[Emp Code]))
RETURN SWITCH(TRUE(),
a_ = 4, "ESCP",
a_ = 1, "E",
a_ = 2 && SP_=2, "ES",
a_ = 2 && EP_=2, "EP",
a_=3 && ESC_=2,"ESC"
)
Capture.JPG 
 
Don't forget to give thumbs up and accept this as a solution if it helped you!!!

Please take a quick glance at newly created dashboards : Restaurant Management Dashboard , HR Analytics Report , Hotel Management Report, Sales Analysis Report , Fortune 500 Companies Analysis , Revenue Tracking Dashboard
Jihwan_Kim
Super User
Super User

Hi,

Please check the below Calculated Column DAX formula and the attached pbix file.

It is for creating a calculated column.

 

Family definition CC =
VAR _conditiontable =
    SUMMARIZE (
        FILTER (
            Data,
            Data[Year] = EARLIER ( Data[Year] )
                && Data[Employee code] = EARLIER ( Data[Employee code] )
        ),
        Data[Relation]
    )
RETURN
    SWITCH (
        TRUE (),
        { "Self" }
            IN _conditiontable
            && { "Spouse" }
            IN _conditiontable
            && { "Children" }
            IN _conditiontable
            && { "Parent" } IN _conditiontable, "ESCP",
        { "Self" }
            IN _conditiontable
            && { "Spouse" }
            IN _conditiontable
            && { "Children" } IN _conditiontable, "ESC",
        { "Self" }
            IN _conditiontable
            && { "Spouse" } IN _conditiontable, "ES",
        { "Self" }
            IN _conditiontable
            && { "Parent" } IN _conditiontable, "EP",
        { "Self" } IN _conditiontable, "E",
        "Check the logic"
    )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

Hi,

 

Thanks a lot. This is exactly what I was looking for.

 

Regards,

Amit Darak

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors