Recent call date & latest agent name - powerquery

Problem statement: For a particular "Id" how can I choose first call date & name of agent who has called recently
Desired output: For Eg: here first call date for id 16468 would be 02-01-2023 & agent who has called recently would be ANGEL which is based on recent call date for that particular id
enter image description here Current scenario
enter image description here Desired output
How can I get above desired output in power query code ?
I tried to create distinct "Id" table and separate Date table with this code but I am not able to display first call date & agent name who has called recently against each distinct "Id"
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Id", Int64.Type}, {"call date", type datetime}, {"Agent name", type text}}),
#"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"call date", type date}}),
Unique_Tablerows = Table.Distinct(Table.SelectColumns(#"Changed Type1", "Id")),
DateOnly_Table = Table.SelectColumns(#"Changed Type1", "call date")
in
DateOnly_Table

Try below. Next time please past samples as text not images
Note your sample output is confused based on your request, since if you are looking for an ID you cant have that repeat twice
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"call date", type date}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Id"}, {
{"call date", each List.Min([call date]), type nullable date},
{"Agent name", each Table.LastN(Table.Sort(_,{{"call date", Order.Ascending}}),1){0}[Agent name], type text}
})
in #"Grouped Rows"

Related

Power query grouping

Can Power query do this?
So I have a group of parent IDs. If the parent Ids are the same but the values from the corresponding attributes are different, I want PQ to let me know they can be grouped together.
Here is the example.
So Parent IDs 12345 are the same, and the values are different, I want the output to say SDSKU..Yes Then if the Parent IDs 333 are the same and values are the same, then that will not be a grouping and I want it to say NO. See image link
If you mean by "values" the values of the column "Color", try the M code below :
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Parent ID", Int64.Type}, {"Kitchen Sink", Int64.Type}, {"Color", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Parent ID", "Kitchen Sink"}, {{"AllData", each _, type table [Parent ID=nullable number, Kitchen Sink=nullable number, Color=nullable text]}, {"OccuID", each Table.RowCount(_), Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "NumberOfColors", each List.Count(List.Distinct([AllData][Color]))),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "SDSKU", each if [OccuID] = [NumberOfColors] then "Yes" else "No"),
#"Expanded AllData" = Table.ExpandTableColumn(#"Added Custom1", "AllData", {"Kitchen Sink", "Color"}, {"Kitchen Sink.1", "Color"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded AllData",{"OccuID", "NumberOfColors"})
in
#"Removed Columns"
If "attributes" are the value of every column except the one named Parent ID, try the M code below :
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source , {"Parent ID"}, {
{"data", each _, type table },
{"check", each if Table.RowCount(_) = Table.RowCount(Table.Distinct(_, List.Difference(Table.ColumnNames(_),{"Parent ID"}))) then "YES" else "NO"}}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", List.Difference(Table.ColumnNames(Source),{"Parent ID"}), List.Difference(Table.ColumnNames(Source),{"Parent ID"}))
in #"Expanded data"

Power Query - running total that resets when values change

I have been searching for a week now and cannot find a resolution to my problem. I have a table which lists the "event" and individual is in during a certain week. I want to add a column - via PowerQuery - that will count the number of weeks a person has been in that event and then resets if the event changes in the following week. For example...
Pers1
Date
Event
Weeks in Event
Pers1
12/22/2022
Consideration
1
Pers1
12/26/2022
Consideration
2
Pers1
1/05/2022
Interview
1
Pers1
1/12/2022
Consideration
1
Pers1
1/19/2022
Consideration
2
Pers1
1/26/2022
Awaiting Hire
1
Pers2
1/19/2022
Awaiting Hire
1
Pers2
1/26/2022
Awaiting Hire
2
Note how the count resets back to starting at 1 when Pers1 has their second stint of Consideration during weeks 1/12 and 1/19. Additionally, I need the solution to be smart enough to distinguish between two different individuals and uniquely count their time in an event.
This community has always come through for me. Please help!
EDIT 1: I incorporated the code provided by Ron and am receiving the following error: Expression.Error: 5 arguments were passed to function which expects between 2 and 4.
Details:
Pattern=
Arguments=List
PQ Advanced Editor Code is below:
let
Source = Excel.Workbook(File.Contents("C:\Location"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Pers1", type text}, {"Date", type date}, {"Event", type text}}),
//add an offset column for Pers and Event to do easy comparison with previous row
offsetPersonEvent=Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[Pers1]) & {null}}
& {List.RemoveFirstN(#"Changed Type"[Event]) & {null}},
type table[Pers=text, Date=date,Event=text,offsetPers=text, offsetEvent=text]
),
//create "grouper" column by checking where both Pers and Event change
#"Added Index" = Table.AddIndexColumn(offsetPersonEvent, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "groups",
each if [Pers]=[offsetPers] and [Event]=[offsetEvent] then null else [Index]),
//remove unneeded columns, fillUp the grouper, and group by "grouper"
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"offsetPers", "offsetEvent", "Index"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"groups"}),
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", {"groups"}, {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1,Int64.Type)
, type table}}),
//Remove unneccessary columns
// Expand the grouped tables
// reset the data types
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"groups"}),
#"Expanded addedIndex" = Table.ExpandTableColumn(#"Removed Columns1", "addedIndex", {"Pers", "Date", "Event", "Weeks in Event"}, {"Pers", "Date", "Event", "Weeks in Event"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded addedIndex",{{"Pers", type text}, {"Date", type date}, {"Event", type text}, {"Weeks in Event", Int64.Type}})
in
#"Changed Type1"
Assuming your data is sorted by Person and then by Date, as you show, you can use the following M-Code.
(If your data is not so sorted, then you could merely add steps initially to sort it appropriately, and then continue with the code shown)
Please read the code comments and examine the Applied steps to understand the algorithm
Open the Advanced Editor and paste in the code below
let
//change table name in next line to your actual table name
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Pers1", type text}, {"Date", type date}, {"Event", type text}}),
//add an offset column for Pers and Event to do easy comparison with previous row
offsetPersonEvent=Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[Pers1]) & {null}}
& {List.RemoveFirstN(#"Changed Type"[Event]) & {null}},
type table[Pers=text, Date=date,Event=text,offsetPers=text, offsetEvent=text]
),
//create "grouper" column by checking where both Pers and Event change
#"Added Index" = Table.AddIndexColumn(offsetPersonEvent, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "groups",
each if [Pers]=[offsetPers] and [Event]=[offsetEvent] then null else [Index]),
//remove unneeded columns, fillUp the grouper, and group by "grouper"
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"offsetPers", "offsetEvent", "Index"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"groups"}),
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", {"groups"}, {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1,Int64.Type)
, type table}}),
//Remove unneccessary columns
// Expand the grouped tables
// reset the data types
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"groups"}),
#"Expanded addedIndex" = Table.ExpandTableColumn(#"Removed Columns1", "addedIndex", {"Pers", "Date", "Event", "Weeks in Event"}, {"Pers", "Date", "Event", "Weeks in Event"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded addedIndex",{{"Pers", type text}, {"Date", type date}, {"Event", type text}, {"Weeks in Event", Int64.Type}})
in
#"Changed Type1"
Ron's code worked with one minor tweak. For me, the following section of code was causing an error
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", {"groups"}, {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1,Int64.Type)
, type table}}),
I removed the Int64.Type parameter from the Table.AddIndexColumn and everything functioned. I've included the updated code snip-it below:
let
Source = Excel.Workbook(File.Contents("C:\Location"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Pers1", type text}, {"Date", type date}, {"Event", type text}}),
//add an offset column for Pers and Event to do easy comparison with previous row
offsetPersonEvent=Table.FromColumns(
Table.ToColumns(#"Changed Type")
& {List.RemoveFirstN(#"Changed Type"[Pers1]) & {null}}
& {List.RemoveFirstN(#"Changed Type"[Event]) & {null}},
type table[Pers=text, Date=date,Event=text,offsetPers=text, offsetEvent=text]
),
//create "grouper" column by checking where both Pers and Event change
#"Added Index" = Table.AddIndexColumn(offsetPersonEvent, "Index", 0, 1),
#"Added Custom" = Table.AddColumn(#"Added Index", "groups",
each if [Pers]=[offsetPers] and [Event]=[offsetEvent] then null else [Index]),
//remove unneeded columns, fillUp the grouper, and group by "grouper"
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"offsetPers", "offsetEvent", "Index"}),
#"Filled Up" = Table.FillUp(#"Removed Columns",{"groups"}),
//Add Index column to each subtable
#"Grouped Rows" = Table.Group(#"Filled Up", "groups", {
{"addedIndex", each Table.AddIndexColumn(_,"Weeks in Event",1,1)
, type table}}),
//Remove unneccessary columns
// Expand the grouped tables
// reset the data types
#"Removed Columns1" = Table.RemoveColumns(#"Grouped Rows",{"groups"}),
#"Expanded addedIndex" = Table.ExpandTableColumn(#"Removed Columns1", "addedIndex", {"Pers", "Date", "Event", "Weeks in Event"}, {"Pers", "Date", "Event", "Weeks in Event"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Expanded addedIndex",{{"Pers", type text}, {"Date", type date}, {"Event", type text}, {"Weeks in Event", Int64.Type}})
in
#"Changed Type1"

Eliminating duplicates error in Visual Studio Development Tool for Tabular and SQL services

I have a question about my M code. I am currently working in Visual Studio and 'Editing Expressions' via VSDT. The problem that I have is that I am trying to create a relationship between one dimension table and another fact table using a primary key. The primary key in the Dim table has unique values, or so it seems. I have removed nulls and blanks in the primary key column. I started off creating the PK by concatenating three columns shared between the two tables. Further, I have removed duplicates and run this command in the PQ editor. I am puzzled as to why, when I join the two tables, I get an error message saying that my Dim table does not have unique values. More frustrating is when I sort the data ascending and descending and I find no blanks, where there once was a blank. I am puzzled about how to fix this problem. I've tried 16 different ways to Sunday-- even moving some of the query steps around. To no avail...
The error thrown:
Error: Failed to save modifications to the server. Column Style_List_PK in Table dStyles contains blank values and this is not
allowed for columns on the one-side of a one-to-many relationship or
for columns that are used as a primary key of a table.
The code:
let
Source = #"SQL/sbsqld59;PJ",
dbo_Style_List = Source{[Schema="dbo",Item="Style_List"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(dbo_Style_List,{{"Brand", type text}, {"Style", type text}, {"Style Description", type text}, {"Sizes", type any}, {"Style Name", type text}, {"General Category", type text}, {"Style Category", type text}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Sizes", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Sizes.1", "Sizes.2", "Sizes.3", "Sizes.4", "Sizes.5", "Sizes.6", "Sizes.7", "Sizes.8", "Sizes.9"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Sizes.1", type text}, {"Sizes.2", type text}, {"Sizes.3", type text}, {"Sizes.4", type text}, {"Sizes.5", type text}, {"Sizes.6", type text}, {"Sizes.7", type text}, {"Sizes.8", type text}, {"Sizes.9", type text}}),
#"Replaced Errors" = Table.ReplaceErrorValues(#"Changed Type1", {{"Sizes.1", "NULL"}, {"Sizes.2", "NULL"}, {"Sizes.3", "NULL"}, {"Sizes.4", "NULL"}, {"Sizes.5", "NULL"}, {"Sizes.6", "NULL"}, {"Sizes.7", "NULL"}, {"Sizes.8", "NULL"}, {"Sizes.9", "NULL"}}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Replaced Errors", {"Brand", "Style", "Style Description", "Style Name", "General Category", "Style Category"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Columns",{"Attribute"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Value", "Size"}}),
#"Replaced Value" = Table.ReplaceValue(#"Renamed Columns","","NULL",Replacer.ReplaceValue,{"Brand", "Style", "Style Description", "Style Name", "General Category", "Style Category", "Size"}),
#"Uppercased Text" = Table.TransformColumns(#"Replaced Value",{{"Style", Text.Upper, type text}, {"Brand", Text.Upper, type text}, {"Size", Text.Upper, type text}}),
#"Added Custom" = Table.AddColumn(#"Uppercased Text", "Style_List_PK", each Text.Combine({[Brand],[Style],[Size]})),
#"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"Style_List_PK", type text}}),
#"Trimmed Text" = Table.TransformColumns(#"Changed Type2",{{"Style_List_PK", Text.Trim, type text}}),
#"Removed Duplicates" = Table.Distinct(#"Trimmed Text", {"Style_List_PK"}),
#"Filtered Rows" = Table.SelectRows(#"Removed Duplicates", each [Style_List_PK] <> null and [Style_List_PK] <> "" and [Style_List_PK] <> " " and [Style_List_PK] <> " ")
in
#"Filtered Rows"

countif formula in power query

I had a formula in a table in excel
=IF([#STATUS]="",[KEY]&"_"&COUNTIF(INDEX([KEY],1):[#KEY],[#KEY]),"")
which showed me how often a value showed in the data. But the same is not working in Power Query
with the formula I use to get if the same value's position in a long data list, and then I use the same in index match formula to find and locate other relevant data
I am trying to achieve:
Date Name Frequency
1/10/2019 Adrian Bartholomeusz 1
1/10/2019 Aditya Tipnis 1
2/10/2019 Abdul Atef 1
2/10/2019 Aditya Tipnis 2
3/10/2019 Abdul Atef 2
In excel I used the formula "=COUNTIF(INDEX([Name],1):[#Name],[#Name])" but when I use the same in Power Query I am getting error
The key steps are:
Add Index
Group Rows
Transform Columns to add a sub-index.
Expand the data back.
The rest are cosmetics.
let
Source = Excel.CurrentWorkbook(),
Table1 = Source{[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Table1, "Index", 0, 1),
#"Grouped Rows" = Table.Group(#"Added Index", {"key"}, {{"Data", each _, type table [key=number, f=text, Index=number]}}),
#"TransformColumns" = Table.TransformColumns(#"Grouped Rows",{"Data", (x) => Table.AddIndexColumn(x, "Index2", 1, 1)}),
#"Expanded Data" = Table.ExpandTableColumn(#"TransformColumns", "Data", {"excel formula", "Index", "Index2"}, {"excel formula", "Index", "Index2"}),
#"Added Custom" = Table.AddColumn(#"Expanded Data", "PQ method", each Text.From([key]) & "_" & Text.From([Index2])),
#"Sorted Rows" = Table.Sort(#"Added Custom",{{"Index", Order.Ascending}}),
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index", "Index2"})
in
#"Removed Columns"

Arranging Data in a Query Table

I'm trying to figure out a simple way (using PowerQuery) to convert this:
into this:
I've spent days trying to figure out a simple way to do it.
Everything I've tried has failed.
You can use Group By on the Transform tab, group by project and define a aggregation for each of the segment columns (e.g. Sum).
Then adjust the created code from List.Sum to List.RemoveNulls.
Then add a column with nested tables from the segment columns, using Table.FromColumns.
Remove the original segment columns and expand the nested tables.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Project", type text}, {"Segment1", type text}, {"Segment2", type text}, {"Segment3", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Project"}, {{"Segment1", each List.RemoveNulls([Segment1]), type text}, {"Segment2", each List.RemoveNulls([Segment2]), type text}, {"Segment3", each List.RemoveNulls([Segment3]), type text}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Tabled", each Table.FromColumns({[Segment1],[Segment2],[Segment3]},{"Segment1","Segment2","Segment3"})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Segment1", "Segment2", "Segment3"}),
#"Expanded Tabled" = Table.ExpandTableColumn(#"Removed Columns", "Tabled", {"Segment1", "Segment2", "Segment3"}, {"Segment1", "Segment2", "Segment3"})
in
#"Expanded Tabled"

Resources