Forum Discussion
Count occurrence of specific string
In Excel i could easily use the wildcard functions like * and ? to establish a calculation, but i am not able to manage this in Power BI with M or DAX.
I have a table with tickets containing information about the steps taken by an agent to resolve an issue, and i have a table with agent names working in our department. On a daily basis i would like to calculate how many tickets an agent has worked on based on the action field, but the field is a large string with different names and dates and including the time in between, exactly in this format "11-07-2024 17:01 Schilder, Sem:" Below is an example of the tables and the counts captured in the name table per day. Hope there is a solution for this as i am struggling for weeks now to get this done in Power BI.
TicketIDAction
| TIC-123 | 11-07-2024 17:01 Schilder, Sem: Gebeld op 5455, helaas geen gehoor. VM ingesproken. Keeping the ticket open for another 5 days Good evening! Kind regards, Services Center 11-07-2024 16:13 Alfie, Dres: Called, 11-07-2024 15:42 Maher, Roos: Wipe nogmaals gestuurd 10-07-2024 12:33 Tucker, Chris: Wipe started |
| TIC-124 | 12-07-2024 12:01 Visser, Jan: Outlook doesnt start correctly 11-07-2024 14:33 Schilder, Sem: Strange issue |
| TIC-125 | 12-07-2024 12:21 Bieber, Justin: Anom Not sure if this is het correct one 10-07-2024 07:33 Depp, Johny: Assigning to another resolver |
| TIC-126 | 12-07-2024 12:33 Tucker, Chris: Error on startup Browser not supported 11-07-2024 09:00 Tucker, Chris: called, no answer 10-07-2024 12:33 Tucker, Chris: Checked browser on startup |
| TIC-127 | 12-07-2024 16:23 Tucker, Chris: resolved 11-07-2024 16:10 Tucker, Chris: wip 11-07-2024 15:25 Tucker, Chris: called, pending 11-07-2024 15:21 Schilder, Sem: : pending user |
DateAgentCountOccurencePerTicket
| 11-7-2024 | Schilder, Sem | 3 |
| 11-7-2024 | Alfie, Dres | 1 |
| 11-7-2024 | Maher, Roos | 1 |
| 11-7-2024 | Visser, Jan | 0 |
| 11-7-2024 | Bieber, Justin | 0 |
| 11-7-2024 | Depp, Johny | 0 |
| 11-7-2024 | Tucker, Chris | 2 |
My bad, here's the revised version.
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], getFields = Table.AddColumn(Source, "Custom", each [ s = List.Select( Text.SplitAny( [Actie], "#(cr)#(lf)"), each Text.EndsWith(Text.Trim(_), ":")), l = Table.FromRecords( List.Transform(s, (x)=> let t = Text.Split(x, " ") in [ Date = t{0}?, Agent = Text.Combine( List.Skip(t, 2), " ") ] )), n = Table.RemoveRowsWithErrors(Table.TransformColumnTypes( l, {{"Date", type date}}), {"Date"}) ][n])[Custom], Combine = Table.Combine(getFields), NoBlanks = Table.SelectRows(Combine, each [Date] <> null and [Date] <> ""), GroupRows = Table.Group(NoBlanks, {"Date", "Agent"}, { {"Count", each Table.RowCount( _ ), Int64.Type} } ) in GroupRows
12 Replies
- PwerQueryKeesSuper User
Have a look at the Text.Contains function. I think it will solve your problem.
- Adem01Frequent Visitor
Its a bit difficult especially because of the timestamp in between. I need to count all data for a specific date and name, so the timestamp is irrelevant.
- m_dekorteResident Rockstar
Hi Adem01
Give this approach a go, except for the Custom Column, it leverages the User Interface.
let Source = Sample, getFields = Table.AddColumn(Source, "Custom", each [ s = Text.Split([Action], " "), t = Date.FromText( s{0} ), a = if try t is date otherwise false then [Date = t, Agent = Text.Combine( List.RemoveMatchingItems(List.Skip(s, 2), {":"}), " ") ] else [Date = null, Agent = null] ][a], type [Date = date, Agent=text] ), ExpandFields = Table.ExpandRecordColumn(getFields, "Custom", {"Date", "Agent"}), NoBlanks = Table.SelectRows(ExpandFields, each [Date] <> null and [Date] <> ""), GroupRows = Table.Group(NoBlanks, {"Date", "Agent"}, { {"Count", each Table.RowCount( _ ), Int64.Type} } ) in GroupRowsWith this result
I hope this is helpful
- PwerQueryKeesSuper User
OK. I see what you mean now. I am not sure I understand the exact format of you data, but...
What I would try:
- Remove the ticket number column
- Split the ticket text on delimeter #(lf) to multiple rows
- Filter the rows you need with Text.Contains on just the names. You need some clever code here, but it feasible.
- Split the timestamp field in a date and time part with Test.SplitByDelimeter
- Group by on the date part and the name part.
If you send an xlsx with test data, I will give it a try....
- Adem01Frequent Visitor
Thanks for the direction, this shows some posibilities but the dataset can be very (200k records) large and this solution will multiply the rows too much. I couldnt attach the file but i guess you can copy the tables inserted.
- m_dekorteResident Rockstar
Hi Adem01,
Thanks for the new sample, give this a go:
let Source = YourSample, getFields = Table.AddColumn(Source, "Custom", each [ s = Text.Split( Text.BeforeDelimiter([Actie], ":", 1), " "), t = Date.FromText( s{0} ), a = if try t is date otherwise false then [Date = t, Agent = Text.Combine( List.Skip(s, 2), " ") ] else [Date = null, Agent = null] ][a], type [Date = date, Agent=text] ), ExpandFields = Table.ExpandRecordColumn(getFields, "Custom", {"Date", "Agent"}), NoBlanks = Table.SelectRows(ExpandFields, each [Date] <> null and [Date] <> ""), GroupRows = Table.Group(NoBlanks, {"Date", "Agent"}, { {"Count", each Table.RowCount( _ ), Int64.Type} } ) in GroupRowsI hope this is helpful