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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
antuneslucas
Frequent Visitor

Create new column based on criteria and previous value

I'm trying to create a column that displays the case id of each event based on Time column.

Each time column Time has a value, it incriment one in the case id.

 

Original table:

Description  Time
B 
C 
D 
E 
F 
G7
H 
I 
J6
K 
L 
M3
N 
O 
P2
Q 
R 
S5
T 

 

Desired table:

Description  Time  CaseID
B 1
C 1
D 1
E 1
F 1
G71
H 2
I 2
J62
K 3
L 3
M33
N 4
O 4
P24
Q 5
R 5
S55
T 6

 

I tried to reach this creating an index column and evaluating if Time column is not null, based on that increase by one the previous case id or keep the previous value, but I was not sucessfull. Mainly because Power Query do not manage tables as Excel do.

 

 

 

if [Time] <> null then [CaseID]{[Index] - 1} + 1 else [CaseID]{[Index] - 1} 

 

 

 

 
 

 

 

1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

in DAX

wdx223_Daniel_0-1700619483938.png

=CALCULATE(COUNT(Table3[Time]),'Table3'[Time]<>0,ALLSELECTED(Table3[Description]))-CALCULATE(COUNT(Table3[Time]),'Table3'[Time]<>0,ALLSELECTED(Table3[Description]),'Table3'[Description]>=MAX('Table3'[Description]))+1

in M

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Custom1 = let a=List.Buffer(Source[ Time]) in List.Generate(()=>{1,0},each _{1}<Table.RowCount(Source),each {_{0}+Byte.From(a{_{1}}<>null),_{1}+1},each _{0}),
    Custom2 = Table.FromColumns(Table.ToColumns(Source)&{Custom1},Table.ColumnNames(Source)&{"CaseID"})
in
    Custom2

wdx223_Daniel_1-1700620038478.png

 

View solution in original post

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

Add an index column to disambiguate.
wdx223_Daniel
Super User
Super User

in DAX

wdx223_Daniel_0-1700619483938.png

=CALCULATE(COUNT(Table3[Time]),'Table3'[Time]<>0,ALLSELECTED(Table3[Description]))-CALCULATE(COUNT(Table3[Time]),'Table3'[Time]<>0,ALLSELECTED(Table3[Description]),'Table3'[Description]>=MAX('Table3'[Description]))+1

in M

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Custom1 = let a=List.Buffer(Source[ Time]) in List.Generate(()=>{1,0},each _{1}<Table.RowCount(Source),each {_{0}+Byte.From(a{_{1}}<>null),_{1}+1},each _{0}),
    Custom2 = Table.FromColumns(Table.ToColumns(Source)&{Custom1},Table.ColumnNames(Source)&{"CaseID"})
in
    Custom2

wdx223_Daniel_1-1700620038478.png

 

Syndicate_Admin
Administrator
Administrator

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Pc23DcAwDAXRVQzWrmxJAzjn3Anafw0TFHDd4/2CMUojpRSSyigt6lCPBjSqfHDmiTqjRRVMK21Du6o2HbQTXarKdNMe9ObPwXk7vzykHw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Description = _t, Time = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each [Time]),
    #"Replaced Value" = Table.ReplaceValue(#"Added Custom"," ",null,Replacer.ReplaceValue,{"Custom"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"Custom"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Custom"}, {{"Rows", each _, type table [Description=nullable text, Time=nullable number]}},GroupKind.Local),
    #"Added Index" = Table.AddIndexColumn(#"Grouped Rows", "CaseID", 1, 1, Int64.Type),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Added Index", "Rows", {"Description", "Time"}, {"Description", "Time"}),
    #"Removed Other Columns" = Table.SelectColumns(#"Expanded Rows",{"Description", "Time", "CaseID"})
in
    #"Removed Other Columns"
AlienSx
Super User
Super User

@antuneslucas 

1. Table.FillUp by [Time] column (providing that you have nulls in there)

2. Group by [Time] column with Operation = "All Rows" in Group By dialog window.

3. Add index column

4. Expand table column.

The second step, group by, would require more than one grouping criteria and I don't think it's possible in my case, because my data can contain different cases with the same time spent. Despite that, thank you very much for your answer.

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! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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