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
 
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
Hello @smozgur,
I was finally able to try these codes and the results have been as expected. Thank you for all the guidance.
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

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