Need assistance with Power Query

jajatidev

Board Regular
Joined
Jul 29, 2016
Messages
78
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I'm looking for assistance to convert the following functions in M Language.

  1. Ageing = IF([ReceiveDate]]=TODAY(),0,NETWORKDAYS([ReceiveDate]]+1,TODAY()))
  2. Age Group =
    IF(AND([Ageing]>=0,[Ageing]<=5),"[0 - 5]",
    IF(AND([Ageing]>=6,[Ageing]<=10),"[6-10]",
    IF(AND([Ageing]>=11,[Ageing]<=20),"[11 - 20]",
    IF(AND([Ageing]>=21,[Ageing]<=30),"[21 - 30]",
    IF(AND([Ageing]>=31,[Ageing]<=60),"[ 31 - 60]",
    IF(AND([Ageing]>=61,[Ageing]<=90),"[ 61 - 90]",
    "[ > 90]"))))))
  3. OrderWeek = [ReceiveDate]-WEEKDAY([ReceiveDate])+2
  4. CRD_Month = IF(OR(ISBLANK([CustomerReqDate]),[CustomerReqDate]<=TODAY()+31),"CRDD Current","CRDD Future")
  5. BlockCode =LET(x,TEXTSPLIT([UserStatus]," "),TEXTJOIN(" ",,XLOOKUP(x,UserStatusCode,UserStatusCode,"")))

For the 5 formula, please find the below table for reference.
SalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusBlockCodeUserStatusCode
1129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP RELINCOINCO
113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCALEOL2 SCALEOL2
1134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP RELINCORRDC
113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCALEOL2 SCALSCAL
1135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDCRRDCXBOP
113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCALEOL2 SCAL
113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCALEOL2 SCAL
113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP RELINCO
1139009462Z600F-KGJ1NC2AP NoMP REL SCALSCAL
1139338933JA05AN-1501NC2AP EOL2 NoMP RELEOL2
113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCALEOL2 SCAL
1139877505RA32F-KGJ1NCOM-GLA NoMP REL
1140021502Z613F-KGJ2NC2AP NoMP REL SCALSCAL
1140021505RA32F-KGJ1NCOM-GLA NoMP REL
1140034522Z600F-KGJ5NC2AP NoMP REL SCALSCAL
1140051802Z600F-KGJ1NC2AP NoMP REL SCALSCAL
114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCALEOL2 SCAL
1101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCALSCAL
110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDCRRDC
1101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCALSCAL
110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDCRRDC
110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
1101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOPXBOP
110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL
1101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOPXBOP
110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL
1104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOPSCAL XBOP
110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
1104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOPSCAL XBOP
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Date columns, CustomerReqDate and ReceiveDate, are missing in your sample data table. So, I have to use sample dates in my source data below.
The current OrderWeek formula doesn't make sense. I assumed you wanted to get the week of the year of the received date.

Here is the base data.
The first table is named as "Data". The second table is named UserStatusCodes contains one column named "UserStatusCode" as in the original sample.
ReceiveDateCustomerReqDateSalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusUserStatusCode
6/22/20239/15/20231129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP RELINCO
7/3/20237/7/2023113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCALEOL2
7/4/20237/8/20231134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP RELRRDC
7/5/20237/9/2023113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCALSCAL
7/6/20237/10/20231135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDCXBOP
7/7/20237/11/2023113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCAL
7/8/20237/12/2023113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCAL
7/9/20237/13/2023113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL
7/10/20237/14/20231139009462Z600F-KGJ1NC2AP NoMP REL SCAL
7/11/20237/15/20231139338933JA05AN-1501NC2AP EOL2 NoMP REL
7/12/20237/16/2023113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL
7/13/20237/17/20231139877505RA32F-KGJ1NCOM-GLA NoMP REL
7/14/20237/18/20231140021502Z613F-KGJ2NC2AP NoMP REL SCAL
7/15/20237/19/20231140021505RA32F-KGJ1NCOM-GLA NoMP REL
7/16/20237/20/20231140034522Z600F-KGJ5NC2AP NoMP REL SCAL
7/17/20237/21/20231140051802Z600F-KGJ1NC2AP NoMP REL SCAL
7/18/20237/22/2023114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL
7/19/20237/23/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL
7/20/20237/24/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC
7/21/20237/25/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL
7/22/20237/26/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC
7/23/20237/27/2023110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
7/24/20237/28/2023110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
7/25/20237/29/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP
7/26/20237/30/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL
7/27/20237/31/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP
7/28/20238/1/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL
7/29/20238/2/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP
7/30/20238/3/2023110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
7/31/20238/4/2023110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
8/1/20238/5/2023110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL
8/2/20238/6/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP


I am not sure about the efficiency of my way of finding the NetworkDays count, but here is my attempt.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode], 
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),
    Ageing = Table.AddColumn(ChangeTypes, "Ageing", 
                    each
                        let
                            startDate = Date.AddDays([ReceiveDate], 1),
                            endDate = Date.From(DateTime.LocalNow()),
                            Result = List.Accumulate(
                                {0..Date.DayOfYear(endDate) - Date.DayOfYear(startDate)},
                                0,
                                (state, current) => if Date.DayOfWeek(Date.AddDays(startDate, current), Day.Sunday) < 1 then state else state + 1
                            )
                        in
                            Result
            ),
    AgeGroup = Table.AddColumn(Ageing, "AgeGroup", 
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek", 
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month", 
                        each 
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31) 
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode", 
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            )
in
    BlockCode

And the result:
ReceiveDateCustomerReqDateSalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusAgeingAgeGroupOrderWeekCRD_MonthBlockCode
6/22/20239/15/20231129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL35[31 - 60]25CRDD FutureINCO
7/3/20237/7/2023113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCAL26[21 - 30]27CRDD CurrentEOL2 SCAL
7/4/20237/8/20231134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL25[21 - 30]27CRDD CurrentINCO
7/5/20237/9/2023113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCAL24[21 - 30]27CRDD CurrentEOL2 SCAL
7/6/20237/10/20231135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDC23[21 - 30]27CRDD CurrentRRDC
7/7/20237/11/2023113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCAL22[21 - 30]27CRDD CurrentEOL2 SCAL
7/8/20237/12/2023113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCAL21[21 - 30]27CRDD CurrentEOL2 SCAL
7/9/20237/13/2023113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL21[21 - 30]28CRDD CurrentINCO
7/10/20237/14/20231139009462Z600F-KGJ1NC2AP NoMP REL SCAL20[11 - 20]28CRDD CurrentSCAL
7/11/20237/15/20231139338933JA05AN-1501NC2AP EOL2 NoMP REL19[11 - 20]28CRDD CurrentEOL2
7/12/20237/16/2023113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL18[11 - 20]28CRDD CurrentEOL2 SCAL
7/13/20237/17/20231139877505RA32F-KGJ1NCOM-GLA NoMP REL17[11 - 20]28CRDD Current
7/14/20237/18/20231140021502Z613F-KGJ2NC2AP NoMP REL SCAL16[11 - 20]28CRDD CurrentSCAL
7/15/20237/19/20231140021505RA32F-KGJ1NCOM-GLA NoMP REL15[11 - 20]28CRDD Current
7/16/20237/20/20231140034522Z600F-KGJ5NC2AP NoMP REL SCAL15[11 - 20]29CRDD CurrentSCAL
7/17/20237/21/20231140051802Z600F-KGJ1NC2AP NoMP REL SCAL14[11 - 20]29CRDD CurrentSCAL
7/18/20237/22/2023114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL13[11 - 20]29CRDD CurrentEOL2 SCAL
7/19/20237/23/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL12[11 - 20]29CRDD CurrentSCAL
7/20/20237/24/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC11[11 - 20]29CRDD CurrentRRDC
7/21/20237/25/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL10[6 - 10]29CRDD CurrentSCAL
7/22/20237/26/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC9[6 - 10]29CRDD CurrentRRDC
7/23/20237/27/2023110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL9[6 - 10]30CRDD Current
7/24/20237/28/2023110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL8[6 - 10]30CRDD Current
7/25/20237/29/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP7[6 - 10]30CRDD CurrentXBOP
7/26/20237/30/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL6[6 - 10]30CRDD Current
7/27/20237/31/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP5[0 - 5]30CRDD CurrentXBOP
7/28/20238/1/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL4[0 - 5]30CRDD Current
7/29/20238/2/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP3[0 - 5]30CRDD CurrentSCAL XBOP
7/30/20238/3/2023110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL3[0 - 5]31CRDD Current
7/31/20238/4/2023110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL2[0 - 5]31CRDD Current
8/1/20238/5/2023110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL1[0 - 5]31CRDD Current
8/2/20238/6/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP0[0 - 5]31CRDD CurrentSCAL XBOP
 
Upvote 0
I just realized that there is a week-of-day mistake in the previous code. The Ageing step should be as follows (only the colored section is changed):

Rich (BB code):
   Ageing = Table.AddColumn(ChangeTypes, "Ageing", 
                    each
                        let
                            startDate = Date.AddDays([ReceiveDate], 1),
                            endDate = Date.From(DateTime.LocalNow()),
                            Result = List.Accumulate(
                                {0..Date.DayOfYear(endDate) - Date.DayOfYear(startDate)},
                                0,
                                (state, current) => if Date.DayOfWeek(Date.AddDays(startDate, current), Day.Monday) > 4 then state else state + 1
                            )
                        in
                            Result
            ),

Also, the following modified version is not using List.Accumulate() function, and it should be much more efficient:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode], 
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),

    Ageing = Table.AddColumn(ChangeTypes, "Ageing", each List.Sum({0} & List.Transform(List.Dates(Date.AddDays([ReceiveDate], 1), Duration.Days(Date.From(DateTime.LocalNow()) - [ReceiveDate]) , #duration(1, 0, 0, 0)), each if Date.DayOfWeek(_, Day.Monday) < 5 then 1 else 0))), 

    AgeGroup = Table.AddColumn(Ageing, "AgeGroup", 
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek", 
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month", 
                        each 
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31) 
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode", 
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            )
in
    BlockCode

And the result:
ReceiveDateCustomerReqDateSalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusAgeingAgeGroupOrderWeekCRD_MonthBlockCode
6/22/20239/15/20231129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL29[21 - 30]25CRDD FutureINCO
7/3/20237/7/2023113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCAL22[21 - 30]27CRDD CurrentEOL2 SCAL
7/4/20237/8/20231134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL21[21 - 30]27CRDD CurrentINCO
7/5/20237/9/2023113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCAL20[11 - 20]27CRDD CurrentEOL2 SCAL
7/6/20237/10/20231135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDC19[11 - 20]27CRDD CurrentRRDC
7/7/20237/11/2023113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/8/20237/12/2023113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/9/20237/13/2023113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL18[11 - 20]28CRDD CurrentINCO
7/10/20237/14/20231139009462Z600F-KGJ1NC2AP NoMP REL SCAL17[11 - 20]28CRDD CurrentSCAL
7/11/20237/15/20231139338933JA05AN-1501NC2AP EOL2 NoMP REL16[11 - 20]28CRDD CurrentEOL2
7/12/20237/16/2023113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL15[11 - 20]28CRDD CurrentEOL2 SCAL
7/13/20237/17/20231139877505RA32F-KGJ1NCOM-GLA NoMP REL14[11 - 20]28CRDD Current
7/14/20237/18/20231140021502Z613F-KGJ2NC2AP NoMP REL SCAL13[11 - 20]28CRDD CurrentSCAL
7/15/20237/19/20231140021505RA32F-KGJ1NCOM-GLA NoMP REL13[11 - 20]28CRDD Current
7/16/20237/20/20231140034522Z600F-KGJ5NC2AP NoMP REL SCAL13[11 - 20]29CRDD CurrentSCAL
7/17/20237/21/20231140051802Z600F-KGJ1NC2AP NoMP REL SCAL12[11 - 20]29CRDD CurrentSCAL
7/18/20237/22/2023114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL11[11 - 20]29CRDD CurrentEOL2 SCAL
7/19/20237/23/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL10[6 - 10]29CRDD CurrentSCAL
7/20/20237/24/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC9[6 - 10]29CRDD CurrentRRDC
7/21/20237/25/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL8[6 - 10]29CRDD CurrentSCAL
7/22/20237/26/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC8[6 - 10]29CRDD CurrentRRDC
7/23/20237/27/2023110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL8[6 - 10]30CRDD Current
7/24/20237/28/2023110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL7[6 - 10]30CRDD Current
7/25/20237/29/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP6[6 - 10]30CRDD CurrentXBOP
7/26/20237/30/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL5[0 - 5]30CRDD Current
7/27/20237/31/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP4[0 - 5]30CRDD CurrentXBOP
7/28/20238/1/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL3[0 - 5]30CRDD Current
7/29/20238/2/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP3[0 - 5]30CRDD CurrentSCAL XBOP
7/30/20238/3/2023110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL3[0 - 5]31CRDD Current
7/31/20238/4/2023110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL2[0 - 5]31CRDD Current
8/1/20238/5/2023110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL1[0 - 5]31CRDD Current
8/2/20238/6/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP0[0 - 5]31CRDD CurrentSCAL XBOP
 
Upvote 1
I just realized that there is a week-of-day mistake in the previous code. The Ageing step should be as follows (only the colored section is changed):

Rich (BB code):
   Ageing = Table.AddColumn(ChangeTypes, "Ageing",
                    each
                        let
                            startDate = Date.AddDays([ReceiveDate], 1),
                            endDate = Date.From(DateTime.LocalNow()),
                            Result = List.Accumulate(
                                {0..Date.DayOfYear(endDate) - Date.DayOfYear(startDate)},
                                0,
                                (state, current) => if Date.DayOfWeek(Date.AddDays(startDate, current), Day.Monday) > 4 then state else state + 1
                            )
                        in
                            Result
            ),

Also, the following modified version is not using List.Accumulate() function, and it should be much more efficient:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),

    Ageing = Table.AddColumn(ChangeTypes, "Ageing", each List.Sum({0} & List.Transform(List.Dates(Date.AddDays([ReceiveDate], 1), Duration.Days(Date.From(DateTime.LocalNow()) - [ReceiveDate]) , #duration(1, 0, 0, 0)), each if Date.DayOfWeek(_, Day.Monday) < 5 then 1 else 0))),

    AgeGroup = Table.AddColumn(Ageing, "AgeGroup",
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek",
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month",
                        each
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31)
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode",
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            )
in
    BlockCode

And the result:
ReceiveDateCustomerReqDateSalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusAgeingAgeGroupOrderWeekCRD_MonthBlockCode
6/22/20239/15/20231129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL29[21 - 30]25CRDD FutureINCO
7/3/20237/7/2023113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCAL22[21 - 30]27CRDD CurrentEOL2 SCAL
7/4/20237/8/20231134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL21[21 - 30]27CRDD CurrentINCO
7/5/20237/9/2023113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCAL20[11 - 20]27CRDD CurrentEOL2 SCAL
7/6/20237/10/20231135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDC19[11 - 20]27CRDD CurrentRRDC
7/7/20237/11/2023113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/8/20237/12/2023113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/9/20237/13/2023113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL18[11 - 20]28CRDD CurrentINCO
7/10/20237/14/20231139009462Z600F-KGJ1NC2AP NoMP REL SCAL17[11 - 20]28CRDD CurrentSCAL
7/11/20237/15/20231139338933JA05AN-1501NC2AP EOL2 NoMP REL16[11 - 20]28CRDD CurrentEOL2
7/12/20237/16/2023113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL15[11 - 20]28CRDD CurrentEOL2 SCAL
7/13/20237/17/20231139877505RA32F-KGJ1NCOM-GLA NoMP REL14[11 - 20]28CRDD Current
7/14/20237/18/20231140021502Z613F-KGJ2NC2AP NoMP REL SCAL13[11 - 20]28CRDD CurrentSCAL
7/15/20237/19/20231140021505RA32F-KGJ1NCOM-GLA NoMP REL13[11 - 20]28CRDD Current
7/16/20237/20/20231140034522Z600F-KGJ5NC2AP NoMP REL SCAL13[11 - 20]29CRDD CurrentSCAL
7/17/20237/21/20231140051802Z600F-KGJ1NC2AP NoMP REL SCAL12[11 - 20]29CRDD CurrentSCAL
7/18/20237/22/2023114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL11[11 - 20]29CRDD CurrentEOL2 SCAL
7/19/20237/23/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL10[6 - 10]29CRDD CurrentSCAL
7/20/20237/24/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC9[6 - 10]29CRDD CurrentRRDC
7/21/20237/25/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL8[6 - 10]29CRDD CurrentSCAL
7/22/20237/26/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC8[6 - 10]29CRDD CurrentRRDC
7/23/20237/27/2023110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL8[6 - 10]30CRDD Current
7/24/20237/28/2023110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL7[6 - 10]30CRDD Current
7/25/20237/29/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP6[6 - 10]30CRDD CurrentXBOP
7/26/20237/30/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL5[0 - 5]30CRDD Current
7/27/20237/31/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP4[0 - 5]30CRDD CurrentXBOP
7/28/20238/1/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL3[0 - 5]30CRDD Current
7/29/20238/2/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP3[0 - 5]30CRDD CurrentSCAL XBOP
7/30/20238/3/2023110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL3[0 - 5]31CRDD Current
7/31/20238/4/2023110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL2[0 - 5]31CRDD Current
8/1/20238/5/2023110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL1[0 - 5]31CRDD Current
8/2/20238/6/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP0[0 - 5]31CRDD CurrentSCAL XBOP
Thank you. Please allow me time to apply the codes as I'm a beginner and just started learning Power BI
 
Upvote 0
I just realized that there is a week-of-day mistake in the previous code. The Ageing step should be as follows (only the colored section is changed):

Rich (BB code):
   Ageing = Table.AddColumn(ChangeTypes, "Ageing",
                    each
                        let
                            startDate = Date.AddDays([ReceiveDate], 1),
                            endDate = Date.From(DateTime.LocalNow()),
                            Result = List.Accumulate(
                                {0..Date.DayOfYear(endDate) - Date.DayOfYear(startDate)},
                                0,
                                (state, current) => if Date.DayOfWeek(Date.AddDays(startDate, current), Day.Monday) > 4 then state else state + 1
                            )
                        in
                            Result
            ),

Also, the following modified version is not using List.Accumulate() function, and it should be much more efficient:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),

    Ageing = Table.AddColumn(ChangeTypes, "Ageing", each List.Sum({0} & List.Transform(List.Dates(Date.AddDays([ReceiveDate], 1), Duration.Days(Date.From(DateTime.LocalNow()) - [ReceiveDate]) , #duration(1, 0, 0, 0)), each if Date.DayOfWeek(_, Day.Monday) < 5 then 1 else 0))),

    AgeGroup = Table.AddColumn(Ageing, "AgeGroup",
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek",
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month",
                        each
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31)
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode",
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            )
in
    BlockCode

And the result:
ReceiveDateCustomerReqDateSalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusAgeingAgeGroupOrderWeekCRD_MonthBlockCode
6/22/20239/15/20231129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL29[21 - 30]25CRDD FutureINCO
7/3/20237/7/2023113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCAL22[21 - 30]27CRDD CurrentEOL2 SCAL
7/4/20237/8/20231134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL21[21 - 30]27CRDD CurrentINCO
7/5/20237/9/2023113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCAL20[11 - 20]27CRDD CurrentEOL2 SCAL
7/6/20237/10/20231135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDC19[11 - 20]27CRDD CurrentRRDC
7/7/20237/11/2023113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/8/20237/12/2023113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/9/20237/13/2023113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL18[11 - 20]28CRDD CurrentINCO
7/10/20237/14/20231139009462Z600F-KGJ1NC2AP NoMP REL SCAL17[11 - 20]28CRDD CurrentSCAL
7/11/20237/15/20231139338933JA05AN-1501NC2AP EOL2 NoMP REL16[11 - 20]28CRDD CurrentEOL2
7/12/20237/16/2023113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL15[11 - 20]28CRDD CurrentEOL2 SCAL
7/13/20237/17/20231139877505RA32F-KGJ1NCOM-GLA NoMP REL14[11 - 20]28CRDD Current
7/14/20237/18/20231140021502Z613F-KGJ2NC2AP NoMP REL SCAL13[11 - 20]28CRDD CurrentSCAL
7/15/20237/19/20231140021505RA32F-KGJ1NCOM-GLA NoMP REL13[11 - 20]28CRDD Current
7/16/20237/20/20231140034522Z600F-KGJ5NC2AP NoMP REL SCAL13[11 - 20]29CRDD CurrentSCAL
7/17/20237/21/20231140051802Z600F-KGJ1NC2AP NoMP REL SCAL12[11 - 20]29CRDD CurrentSCAL
7/18/20237/22/2023114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL11[11 - 20]29CRDD CurrentEOL2 SCAL
7/19/20237/23/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL10[6 - 10]29CRDD CurrentSCAL
7/20/20237/24/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC9[6 - 10]29CRDD CurrentRRDC
7/21/20237/25/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL8[6 - 10]29CRDD CurrentSCAL
7/22/20237/26/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC8[6 - 10]29CRDD CurrentRRDC
7/23/20237/27/2023110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL8[6 - 10]30CRDD Current
7/24/20237/28/2023110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL7[6 - 10]30CRDD Current
7/25/20237/29/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP6[6 - 10]30CRDD CurrentXBOP
7/26/20237/30/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL5[0 - 5]30CRDD Current
7/27/20237/31/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP4[0 - 5]30CRDD CurrentXBOP
7/28/20238/1/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL3[0 - 5]30CRDD Current
7/29/20238/2/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP3[0 - 5]30CRDD CurrentSCAL XBOP
7/30/20238/3/2023110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL3[0 - 5]31CRDD Current
7/31/20238/4/2023110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL2[0 - 5]31CRDD Current
8/1/20238/5/2023110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL1[0 - 5]31CRDD Current
8/2/20238/6/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP0[0 - 5]31CRDD CurrentSCAL XBOP
Thanks. I was able to understand and apply the queries for desired results. Still, a long way to go.
 
Upvote 0
Thanks. I was able to understand and apply the queries for desired results. Still, a long way to go.
You're welcome.
I am also glad to hear that you understand the code as there are many useful functions and methods in it. Although I posted the second code to make it more efficient without using List.Accumulate, you will find the List.Accumulate function is also a very powerful function once you understand how it works.

Thanks for the feedback.
 
Upvote 0
You're welcome.
I am also glad to hear that you understand the code as there are many useful functions and methods in it. Although I posted the second code to make it more efficient without using List.Accumulate, you will find the List.Accumulate function is also a very powerful function once you understand how it works.

Thanks for the feedback.
it's been a learning curve. I have been slow to revert back as I'm trying to learn before implementing.
I have in fact used the second code without the List.Accumulate for Ageing.

Thanks again. Today I have posted a new string requesting assistance with 3 excel formulas.
Hoping you would take a look at it because I came across an old post on SUMIF which I tried to replicate but couldn't make much progress.
 
Upvote 0
I just realized that there is a week-of-day mistake in the previous code. The Ageing step should be as follows (only the colored section is changed):

Rich (BB code):
   Ageing = Table.AddColumn(ChangeTypes, "Ageing",
                    each
                        let
                            startDate = Date.AddDays([ReceiveDate], 1),
                            endDate = Date.From(DateTime.LocalNow()),
                            Result = List.Accumulate(
                                {0..Date.DayOfYear(endDate) - Date.DayOfYear(startDate)},
                                0,
                                (state, current) => if Date.DayOfWeek(Date.AddDays(startDate, current), Day.Monday) > 4 then state else state + 1
                            )
                        in
                            Result
            ),

Also, the following modified version is not using List.Accumulate() function, and it should be much more efficient:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),

    Ageing = Table.AddColumn(ChangeTypes, "Ageing", each List.Sum({0} & List.Transform(List.Dates(Date.AddDays([ReceiveDate], 1), Duration.Days(Date.From(DateTime.LocalNow()) - [ReceiveDate]) , #duration(1, 0, 0, 0)), each if Date.DayOfWeek(_, Day.Monday) < 5 then 1 else 0))),

    AgeGroup = Table.AddColumn(Ageing, "AgeGroup",
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek",
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month",
                        each
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31)
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode",
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            )
in
    BlockCode

And the result:
ReceiveDateCustomerReqDateSalesOrderNoProductNoOrderQtyCompleteDeliveryUserStatusAgeingAgeGroupOrderWeekCRD_MonthBlockCode
6/22/20239/15/20231129353801NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL29[21 - 30]25CRDD FutureINCO
7/3/20237/7/2023113430548A1A30A-KGJ1NBLCA C2AP EOL2 NoMP REL SCAL22[21 - 30]27CRDD CurrentEOL2 SCAL
7/4/20237/8/20231134937101NR73A-K1H1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL21[21 - 30]27CRDD CurrentINCO
7/5/20237/9/2023113512547A1A30A-KGJ1NC2AP EOL2 NoMP REL SCAL20[11 - 20]27CRDD CurrentEOL2 SCAL
7/6/20237/10/20231135438135RA32F-KGJ1NC2AP MDID NoMP REL RRDC19[11 - 20]27CRDD CurrentRRDC
7/7/20237/11/2023113561139A1A30A-KGJ1NEOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/8/20237/12/2023113705782A1A77A-KGJ10NC2AP EOL2 MDID NoMP REL SCAL18[11 - 20]27CRDD CurrentEOL2 SCAL
7/9/20237/13/2023113725103N7L37A1NC2AP INC-VBAP-CEPOK INCO MDID NoMP REL18[11 - 20]28CRDD CurrentINCO
7/10/20237/14/20231139009462Z600F-KGJ1NC2AP NoMP REL SCAL17[11 - 20]28CRDD CurrentSCAL
7/11/20237/15/20231139338933JA05AN-1501NC2AP EOL2 NoMP REL16[11 - 20]28CRDD CurrentEOL2
7/12/20237/16/2023113969730A1A56A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL15[11 - 20]28CRDD CurrentEOL2 SCAL
7/13/20237/17/20231139877505RA32F-KGJ1NCOM-GLA NoMP REL14[11 - 20]28CRDD Current
7/14/20237/18/20231140021502Z613F-KGJ2NC2AP NoMP REL SCAL13[11 - 20]28CRDD CurrentSCAL
7/15/20237/19/20231140021505RA32F-KGJ1NCOM-GLA NoMP REL13[11 - 20]28CRDD Current
7/16/20237/20/20231140034522Z600F-KGJ5NC2AP NoMP REL SCAL13[11 - 20]29CRDD CurrentSCAL
7/17/20237/21/20231140051802Z600F-KGJ1NC2AP NoMP REL SCAL12[11 - 20]29CRDD CurrentSCAL
7/18/20237/22/2023114016353A1A77A-KGJ1NC2AP EOL2 MDID NoMP REL SCAL11[11 - 20]29CRDD CurrentEOL2 SCAL
7/19/20237/23/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL10[6 - 10]29CRDD CurrentSCAL
7/20/20237/24/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC9[6 - 10]29CRDD CurrentRRDC
7/21/20237/25/20231101134333GZ25A-KGJ1YBLKD C2AP COM-GLA NoMP REL SCAL8[6 - 10]29CRDD CurrentSCAL
7/22/20237/26/2023110113433K5L35A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL RRDC8[6 - 10]29CRDD CurrentRRDC
7/23/20237/27/2023110113433Y3D03A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL8[6 - 10]30CRDD Current
7/24/20237/28/2023110113433Z7C07A1YBLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL7[6 - 10]30CRDD Current
7/25/20237/29/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP6[6 - 10]30CRDD CurrentXBOP
7/26/20237/30/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL5[0 - 5]30CRDD Current
7/27/20237/31/20231101134333PZ35A-KGJ1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP PREL REL XBOP4[0 - 5]30CRDD CurrentXBOP
7/28/20238/1/2023110113433D7P27A1YC2AP CFSL COM-FDL COM-GLA COM-PDD NoMP REL3[0 - 5]30CRDD Current
7/29/20238/2/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP3[0 - 5]30CRDD CurrentSCAL XBOP
7/30/20238/3/2023110433907K5L35A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL3[0 - 5]31CRDD Current
7/31/20238/4/2023110433907Y3D03A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL2[0 - 5]31CRDD Current
8/1/20238/5/2023110433907Z7C07A1YBLCA BLKD C2AP CFSL COM-ATP COM-FDL COM-GLA NoMP REL1[0 - 5]31CRDD Current
8/2/20238/6/20231104339073GZ25A-KGJ1YBLCA BLKD C2AP COM-GLA NoMP REL SCAL XBOP0[0 - 5]31CRDD CurrentSCAL XBOP
Hi @smozgur,
I'm reassessing the UserStatus column to change the approach of capturing the BlockCode. I need to look at the UserStatus field and retain the Block Codes that appear after "REL".
 
Upvote 0
What I understand is you want to list the status codes right after REL status code, and it can be done by adding a new column by first splitting the codes as a list, find the position of the REL code, and finally remove the list items by using that position value. I used the final code I provided above as I am not sure what you'd like to do with the rest of the initial application logic. I believe you can use the method which I explained and added as the last step to the existing code and generate the logic if any.

If you have further questions about this new requirement, then it is best to start a new thread to explain it, so other Power Query users on the board can see your question to respond and you might receive better answers instead of mine only.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode], 
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),
    Ageing = Table.AddColumn(ChangeTypes, "Ageing", each List.Sum({0} & List.Transform(List.Dates(Date.AddDays([ReceiveDate], 1), Duration.Days(Date.From(DateTime.LocalNow()) - [ReceiveDate]) , #duration(1, 0, 0, 0)), each if Date.DayOfWeek(_, Day.Monday) < 5 then 1 else 0))), 
    AgeGroup = Table.AddColumn(Ageing, "AgeGroup", 
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek", 
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month", 
                        each 
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31) 
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode", 
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            ),
    AfterRel = Table.AddColumn(BlockCode, "AfterREL", 
                    each 
                        let 
                            UserStatusCodes = Text.Split([UserStatus], " "),
                            RelPosition = List.PositionOf(UserStatusCodes, "REL"),
                            AfterRel = Text.Combine(List.RemoveFirstN(UserStatusCodes, RelPosition + 1), " ")
                        in
                            AfterRel
                    )
in
    AfterRel
 
Upvote 1
Solution
What I understand is you want to list the status codes right after REL status code, and it can be done by adding a new column by first splitting the codes as a list, find the position of the REL code, and finally remove the list items by using that position value. I used the final code I provided above as I am not sure what you'd like to do with the rest of the initial application logic. I believe you can use the method which I explained and added as the last step to the existing code and generate the logic if any.

If you have further questions about this new requirement, then it is best to start a new thread to explain it, so other Power Query users on the board can see your question to respond and you might receive better answers instead of mine only.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    UserStatusCodes = Excel.CurrentWorkbook(){[Name="UserStatusCodes"]}[Content][UserStatusCode],
    ChangeTypes = Table.TransformColumnTypes(Source,{{"ReceiveDate", type date}, {"CustomerReqDate", type date}, {"SalesOrderNo", type text}, {"ProductNo", type text}, {"OrderQty", Int64.Type}, {"CompleteDelivery", type text}, {"UserStatus", type text}}),
    Ageing = Table.AddColumn(ChangeTypes, "Ageing", each List.Sum({0} & List.Transform(List.Dates(Date.AddDays([ReceiveDate], 1), Duration.Days(Date.From(DateTime.LocalNow()) - [ReceiveDate]) , #duration(1, 0, 0, 0)), each if Date.DayOfWeek(_, Day.Monday) < 5 then 1 else 0))),
    AgeGroup = Table.AddColumn(Ageing, "AgeGroup",
                        each
                            if List.Contains({0..5}, [Ageing]) then "[0 - 5]"
                            else if List.Contains({6..10}, [Ageing]) then "[6 - 10]"
                            else if List.Contains({10..20}, [Ageing]) then "[11 - 20]"
                            else if List.Contains({21..30}, [Ageing]) then "[21 - 30]"
                            else if List.Contains({31..60}, [Ageing]) then "[31 - 60]"
                            else if List.Contains({61..90}, [Ageing]) then "[61 - 90]"
                            else "[ > 90]"
                ),
    OrderWeek = Table.AddColumn(AgeGroup, "OrderWeek",
                        each Date.WeekOfYear(Date.AddDays([ReceiveDate], 2 - Date.DayOfWeek([ReceiveDate])))
                ),
    CRD_Month = Table.AddColumn(OrderWeek, "CRD_Month",
                        each
                            if [CustomerReqDate] is null or [CustomerReqDate] <= Date.AddDays(Date.From(DateTime.LocalNow()), 31)
                            then "CRDD Current"
                            else "CRDD Future"
                ),
    BlockCode = Table.AddColumn(CRD_Month, "BlockCode",
                    each Text.Combine(
                            List.Intersect({Text.Split([UserStatus], " "), UserStatusCodes}),
                            " "
                    )
            ),
    AfterRel = Table.AddColumn(BlockCode, "AfterREL",
                    each
                        let
                            UserStatusCodes = Text.Split([UserStatus], " "),
                            RelPosition = List.PositionOf(UserStatusCodes, "REL"),
                            AfterRel = Text.Combine(List.RemoveFirstN(UserStatusCodes, RelPosition + 1), " ")
                        in
                            AfterRel
                    )
in
    AfterRel
Thanks, @smozgur,
Couldnt respond sooner, as I was preoccupied and just got back to focusing on the developments I have been pursuing.
I'll run the code and let you know. And I'm sure it's gonna work as expected.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top