Excel Formulas to Power Query formulas Help

%3CLINGO-SUB%20id%3D%22lingo-sub-2056229%22%20slang%3D%22en-US%22%3EExcel%20Formulas%20to%20Power%20Query%20formulas%20Help%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2056229%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%3EI%E2%80%99m%20having%20troubles%20with%20the%20formulas%20I%20created%20in%20Excel%20then%20going%20to%20Power%20Query%20for%20my%20first%20time.%20After%20creating%20and%20modeling%20in%20Excel%2C%20I'm%20not%20sure%20how%20to%20go%20about%26nbsp%3Bmaking%20this%26nbsp%3Bpossible%20in%20PQ.%20How%20do%20I%20go%20from%20an%20Excel%20formula%20to%20an%20M%20code%20equivalent%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3COL%3E%3CLI%3E%3CSPAN%3EUse%20IF(OR(%20to%20determine%20type%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%3DIF(OR(A2%3D999999%2CA2%3D444444)%2C%22Type%201%22%2C%22Type%202%22)%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3COL%3E%3CLI%3E%3CSPAN%3EUse%20the%20result%20of%20the%20first%20formula%20in%20an%20if%20and%20if%20false%20then%20VLOOKUP%20IF(VLOOKUP(.%20How%20Would%20I%20go%20from%20an%20IF%20with%20a%20false%20being%20an%20function%20(in%20this%20case%20VLOOKUP)%3F%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%3DIFERROR(IF(D2%3D%22Type%201%22%2C%22Other%22%2CVLOOKUP(B2%2CTable2%2C2%2C0))%2C%22-%22)%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3COL%3E%3CLI%3E%3CSPAN%3ETake%20a%20short%20date%20MM%2FDD%2FYYYY%20and%20get%20week%20number%20starting%20on%20Monday%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%3DWEEKNUM(C2%2C21)%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3COL%3E%3CLI%3E%3CSPAN%3EMapping%20calendar%20days%20to%20conform%20to%20a%204-4-5%20accounting%20calendar%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%3DIFERROR(IFS(AND(WEEKNUM(C2)%26gt%3B%3D1%2CWEEKNUM(C2)%26lt%3B%3D4)%2C%221%22%2CAND(WEEKNUM(C2)%26gt%3B%3D5%2CWEEKNUM(C2)%26lt%3B%3D8)%2C%222%22%2CAND(WEEKNUM(C2)%26gt%3B%3D9%2CWEEKNUM(C2)%26lt%3B%3D13)%2C%223%22%2CAND(WEEKNUM(C2)%26gt%3B%3D14%2CWEEKNUM(C2)%26lt%3B%3D17)%2C%224%22%2CAND(WEEKNUM(C2)%26gt%3B%3D18%2CWEEKNUM(C2)%26lt%3B%3D21)%2C%225%22%2CAND(WEEKNUM(C2)%26gt%3B%3D22%2CWEEKNUM(C2)%26lt%3B%3D26)%2C%226%22%2CAND(WEEKNUM(C2)%26gt%3B%3D27%2CWEEKNUM(C2)%26lt%3B%3D30)%2C%227%22%2CAND(WEEKNUM(C2)%26gt%3B%3D31%2CWEEKNUM(C2)%26lt%3B%3D34)%2C%228%22%2CAND(WEEKNUM(C2)%26gt%3B%3D35%2CWEEKNUM(C2)%26lt%3B%3D39)%2C%229%22%2CAND(WEEKNUM(C2)%26gt%3B%3D40%2CWEEKNUM(C2)%26lt%3B%3D43)%2C%2210%22%2CAND(WEEKNUM(C2)%26gt%3B%3D44%2CWEEKNUM(C2)%26lt%3B%3D47)%2C%2211%22%2CAND(WEEKNUM(C2)%26gt%3B%3D48%2CWEEKNUM(C2)%26lt%3B%3D53)%2C%2212%22)%2C%22-%22)%3C%2FP%3E%3CP%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3COL%3E%3CLI%3E%3CSPAN%3EKnow%20If%20data%20is%20from%20this%20year%2C%20last%20year%2C%202%20years%20ago%2C%203%20year%20ago%2C%20or%20older%3C%2FSPAN%3E%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%3DIFERROR(IFS(AND(ABS(%24C2-TODAY())%26gt%3B%3D1%2CABS(%24C2-TODAY())%26lt%3B%3D365)%2C12%2CAND(ABS(%24C2-TODAY())%26gt%3B%3D366%2CABS(%24C2-TODAY())%26lt%3B%3D730)%2C24%2CAND(ABS(%24C2-TODAY())%26gt%3B%3D731%2CABS(%24C2-TODAY())%26lt%3B%3D1095)%2C36%2CAND(ABS(%24C2-TODAY())%26gt%3B%3D1096%2CABS(%24C2-TODAY())%26lt%3B%3D1491)%2C48%2CAND(ABS(%24C2-TODAY())%26gt%3B%3D1492%2CABS(%24C2-TODAY())%26lt%3B%3D3500)%2C%22%26gt%3B48%22)%2C%22-%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2056229%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2056562%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Formulas%20to%20Power%20Query%20formulas%20Help%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2056562%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F830570%22%20target%3D%22_blank%22%3E%40Jpalaci1%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPower%20Query%20has%20another%20logic%2C%20it's%20note%20necessary%20to%20map%20Excel%20formulas.%20First%20part%20could%20be%20as%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powerquery%22%3E%3CCODE%3Elet%0A%20%20%20%20Source%20%3D%20Table1%2C%0A%20%20%20%20%23%22Removed%20Other%20Columns%22%20%3D%20Table.SelectColumns(Source%2C%7B%22Account%20Number%22%2C%20%22Sales%20Area%20%23%22%2C%20%22Date%22%7D)%2C%0A%20%20%20%20%23%22Merged%20Queries%22%20%3D%20Table.NestedJoin(%0A%20%20%20%20%20%20%20%20%23%22Removed%20Other%20Columns%22%2C%20%7B%22Sales%20Area%20%23%22%7D%2C%0A%20%20%20%20%20%20%20%20Table2%2C%20%7B%22%23%22%7D%2C%20%22Table2%22%2C%0A%20%20%20%20%20%20%20%20JoinKind.LeftOuter%0A%20%20%20%20)%2C%0A%20%20%20%20%23%22Expanded%20Table2%22%20%3D%20Table.ExpandTableColumn(%23%22Merged%20Queries%22%2C%20%22Table2%22%2C%20%7B%22Area%22%7D%2C%20%7B%22Area%22%7D)%2C%0A%20%20%20%20%23%22Replaced%20Value%22%20%3D%20Table.ReplaceValue(%0A%20%20%20%20%20%20%20%20%23%22Expanded%20Table2%22%2C%0A%20%20%20%20%20%20%20%20each%20%5BArea%5D%2C%0A%20%20%20%20%20%20%20%20each%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20%5BAccount%20Number%5D%3D999999%20or%20%5BAccount%20Number%5D%3D444444%0A%20%20%20%20%20%20%20%20%20%20%20%20then%20%22Other%22%0A%20%20%20%20%20%20%20%20%20%20%20%20else%20%5BArea%5D%2C%0A%20%20%20%20Replacer.ReplaceValue%2C%7B%22Area%22%7D%0A)%2C%0A%20%20%20%20%23%22Inserted%20Week%20of%20Year%22%20%3D%20Table.AddColumn(%0A%20%20%20%20%20%20%20%20%23%22Replaced%20Value%22%2C%0A%20%20%20%20%20%20%20%20%22Week%20of%20Year%22%2C%0A%20%20%20%20%20%20%20%20each%20Date.WeekOfYear(%5BDate%5D)%2C%20Int64.Type)%2C%0A%20%20%20%20%23%22Changed%20Type%22%20%3D%20Table.TransformColumnTypes(%23%22Inserted%20Week%20of%20Year%22%2C%7B%7B%22Date%22%2C%20type%20date%7D%7D)%0Ain%0A%20%20%20%20%23%22Changed%20Type%22%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3Epractically%20everything%20from%20UI.%20Not%20sure%20right%20now%20about%20other%20calendars%2C%20perhaps%20mapping%20tables%20could%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2057299%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Formulas%20to%20Power%20Query%20formulas%20Help%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2057299%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F521%22%20target%3D%22_blank%22%3E%40Sergei%20Baklan%3C%2FA%3E%26nbsp%3BThank%20you%20so%20much!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20wasn't%20at%20first%20mapping%2Fcreating%20everything%20in%20Excel%20first%20rather%20taking%20what%20I%20have%20in%20Excel%20then%20turning%20that%20in%20Power%20Query%20since%20I%20already%20finished%20and%20know%20this%20is%20what%20I%20want%20to%20get%20to.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I’m having troubles with the formulas I created in Excel then going to Power Query for my first time. After creating and modeling in Excel, I'm not sure how to go about making this possible in PQ. How do I go from an Excel formula to an M code equivalent?

 

  1. Use IF(OR( to determine type

=IF(OR(A2=999999,A2=444444),"Type 1","Type 2")

 

  1. Use the result of the first formula in an if and if false then VLOOKUP IF(VLOOKUP(. How Would I go from an IF with a false being an function (in this case VLOOKUP)?

=IFERROR(IF(D2="Type 1","Other",VLOOKUP(B2,Table2,2,0)),"-")

 

  1. Take a short date MM/DD/YYYY and get week number starting on Monday

=WEEKNUM(C2,21)

 

  1. Mapping calendar days to conform to a 4-4-5 accounting calendar

=IFERROR(IFS(AND(WEEKNUM(C2)>=1,WEEKNUM(C2)<=4),"1",AND(WEEKNUM(C2)>=5,WEEKNUM(C2)<=8),"2",AND(WEEKNUM(C2)>=9,WEEKNUM(C2)<=13),"3",AND(WEEKNUM(C2)>=14,WEEKNUM(C2)<=17),"4",AND(WEEKNUM(C2)>=18,WEEKNUM(C2)<=21),"5",AND(WEEKNUM(C2)>=22,WEEKNUM(C2)<=26),"6",AND(WEEKNUM(C2)>=27,WEEKNUM(C2)<=30),"7",AND(WEEKNUM(C2)>=31,WEEKNUM(C2)<=34),"8",AND(WEEKNUM(C2)>=35,WEEKNUM(C2)<=39),"9",AND(WEEKNUM(C2)>=40,WEEKNUM(C2)<=43),"10",AND(WEEKNUM(C2)>=44,WEEKNUM(C2)<=47),"11",AND(WEEKNUM(C2)>=48,WEEKNUM(C2)<=53),"12"),"-")

 

  1. Know If data is from this year, last year, 2 years ago, 3 year ago, or older

=IFERROR(IFS(AND(ABS($C2-TODAY())>=1,ABS($C2-TODAY())<=365),12,AND(ABS($C2-TODAY())>=366,ABS($C2-TODAY())<=730),24,AND(ABS($C2-TODAY())>=731,ABS($C2-TODAY())<=1095),36,AND(ABS($C2-TODAY())>=1096,ABS($C2-TODAY())<=1491),48,AND(ABS($C2-TODAY())>=1492,ABS($C2-TODAY())<=3500),">48"),"-")

 

Thank you!

2 Replies

@Jpalaci1 

Power Query has another logic, it's note necessary to map Excel formulas. First part could be as

let
    Source = Table1,
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Account Number", "Sales Area #", "Date"}),
    #"Merged Queries" = Table.NestedJoin(
        #"Removed Other Columns", {"Sales Area #"},
        Table2, {"#"}, "Table2",
        JoinKind.LeftOuter
    ),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Area"}, {"Area"}),
    #"Replaced Value" = Table.ReplaceValue(
        #"Expanded Table2",
        each [Area],
        each
            if [Account Number]=999999 or [Account Number]=444444
            then "Other"
            else [Area],
    Replacer.ReplaceValue,{"Area"}
),
    #"Inserted Week of Year" = Table.AddColumn(
        #"Replaced Value",
        "Week of Year",
        each Date.WeekOfYear([Date]), Int64.Type),
    #"Changed Type" = Table.TransformColumnTypes(#"Inserted Week of Year",{{"Date", type date}})
in
    #"Changed Type"

practically everything from UI. Not sure right now about other calendars, perhaps mapping tables could help.

@Sergei Baklan Thank you so much!

 

I wasn't at first mapping/creating everything in Excel first rather taking what I have in Excel then turning that in Power Query since I already finished and know this is what I want to get to.