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
Charlesdudu
Regular Visitor

New column with first occurences of a value in a column with filters

Hello community,

 

This is my first post here and  I didn't find on the forum how to resolve my problem.

 

I have a data source which is structured like this :

Format données PBI.jpg

 

I need to create 2 new columns :

  • A new column that gives me the value of the column "Valeur" for the first occurrence of "SV" in the column "Type" for each value of the column "Clé train"
  • A new column that gives the value of the column "Valeur" for the second occurrence of "EV" in the column "Type" for each value of the column "Clé train"

 

The result should be "0000108780" for the first column and "0000063881" for the 2nd column for the example given.

 

I have done plenty of tests with filters functions but don't find a correct approach.

 

Thank you for your help !

 

Regards

 

Charles

1 ACCEPTED SOLUTION
v-zhenbw-msft
Community Support
Community Support

Hi @Charlesdudu ,

 

We can add an index column and create three calculate columns to meet your requirement.

 

1. We need to add an index column in Power Query Editor.

 

new1.jpg

 

2. Then we close and apply, create a rank column using DAX function.

 

Column = 
RANKX(FILTER('Table','Table'[TYPE]=EARLIER('Table'[TYPE])),'Table'[Index],,ASC,Dense)

 

new2.jpg

 

3. At last we can create two columns to get the result.

 

New column SV = CALCULATE(MAX('Table'[value]),FILTER('Table','Table'[Column]=1&&'Table'[TYPE]="SV"))

 

New column EV = CALCULATE(MAX('Table'[value]),FILTER('Table','Table'[Column]=2&&'Table'[TYPE]="EV"))

 

new3.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

BTW, pbix as attached.

View solution in original post

2 REPLIES 2
v-zhenbw-msft
Community Support
Community Support

Hi @Charlesdudu ,

 

We can add an index column and create three calculate columns to meet your requirement.

 

1. We need to add an index column in Power Query Editor.

 

new1.jpg

 

2. Then we close and apply, create a rank column using DAX function.

 

Column = 
RANKX(FILTER('Table','Table'[TYPE]=EARLIER('Table'[TYPE])),'Table'[Index],,ASC,Dense)

 

new2.jpg

 

3. At last we can create two columns to get the result.

 

New column SV = CALCULATE(MAX('Table'[value]),FILTER('Table','Table'[Column]=1&&'Table'[TYPE]="SV"))

 

New column EV = CALCULATE(MAX('Table'[value]),FILTER('Table','Table'[Column]=2&&'Table'[TYPE]="EV"))

 

new3.jpg

 

If it doesn’t meet your requirement, could you please show the exact expected result based on the table that you have shared?

 

Best regards,

 

Community Support Team _ zhenbw

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

BTW, pbix as attached.

DataInsights
Super User
Super User

@Charlesdudu, try these calculated columns:

 

Valeur First SV = 
VAR vCleTrain = TestData[Cle Train]
VAR vCleTrainTable =
    FILTER ( TestData, TestData[Cle Train] = vCleTrain && TestData[Type] = "SV" )
VAR vResult =
    MINX ( vCleTrainTable, TestData[Valeur] )
RETURN
    vResult

Valeur Second EV = 
VAR vCleTrain = TestData[Cle Train]
VAR vCleTrainTable =
    FILTER ( TestData, TestData[Cle Train] = vCleTrain && TestData[Type] = "EV" )
VAR vRankedTable =
    ADDCOLUMNS (
        vCleTrainTable,
        "Rank", RANKX ( vCleTrainTable, TestData[Valeur],, ASC, DENSE )
    )
VAR vTargetRow =
    FILTER ( vRankedTable, [Rank] = 2 )
VAR vResult =
    MAXX ( vTargetRow, TestData[Valeur] )
RETURN
    vResult

 

DataInsights_0-1601560194507.png

 





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

Proud to be a Super User!




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.