Help with power query formula

mark84

New Member
Joined
Jan 22, 2021
Messages
31
Office Version
  1. 2016
Platform
  1. Windows
Hi eveybody,
I'm trying to transform an excel formula in a power query if statements.
This is the excel formula:

=IF(J2=1;IF(AND(D2=" P";W2="S");ROUND(G2*(1-(N2/10000))-(G2*0,6%);2);IF(AND(D2=" P";W2<>"S");
ROUND($G2*(1-($N2/10000));2);IF($D2=" N";ROUND((G2/(1+(I2/100)))*(1-(N2/10000));2);IF(D2=" D";N2/100))));"")


I've tried to use this in power query:
= if [#" Qty1"]=1 and [#" Dove"]="P" and [Aifa]="S" then Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000))-(([#" PrezPub"]*0.6/100)),2) else if [#" Qty1"]=1 and [#" Dove"]="P" and [Aifa]<>"S" then Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000)),2) else if [#" Qty1"]=1 and [#" Dove"]="N" and [Aifa]<>"S" then Number.Round(([#" PrezPub"]/(1+[#" CodIva"]/100))*(1-([#" Val1"]/10000)),2) else if [#" Qty1"]=1 and [#" Dove"]="D" then [#" PrezPub"]/100 else ""

but I know that it's not possible to use more than one AND condition.

So I tried in this other way but I don't get the same result of the excel formula.

= if [#" Qty1"]=1 then (if [#" Dove"]="P" and [Aifa]="S" then Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000))-(([#" PrezPub"]*0.6/100)),2) else if [#" Dove"]="P" and [Aifa]<>"S" then Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000)),2) else if [#" Dove"]="N" and [Aifa]<>"S" then Number.Round(([#" PrezPub"]/(1+[#" CodIva"]/100))*(1-([#" Val1"]/10000)),2) else if [#" Dove"]="D" then [#" PrezPub"]/100 else "") else ""

I'm going crazy! :)

Could you help me please?

Thank you
Regards

Marco
 

Attachments

  • img 3.JPG
    img 3.JPG
    153.7 KB · Views: 5
Last edited:

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Maybe it's a version issue because your formula works for me. But if it's true that your version is limited to one "and" then you can try the second version:

Your formula:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{" Qty1", type number}, {" PrezPub", type number}, {" CodIva", type number}}),
    Custom = Table.AddColumn(ChangeType, "Custom", each 
        if [#" Qty1"]=1 and [#" Dove"]="P" and [Aifa]="S" then 
            Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000))-(([#" PrezPub"]*0.6/100)),2) 
        else if [#" Qty1"]=1 and [#" Dove"]="P" and [Aifa]<>"S" then 
            Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000)),2) 
        else if [#" Qty1"]=1 and [#" Dove"]="N" and [Aifa]<>"S" then 
            Number.Round(([#" PrezPub"]/(1+[#" CodIva"]/100))*(1-([#" Val1"]/10000)),2) 
        else if [#" Qty1"]=1 and [#" Dove"]="D" then 
            [#" PrezPub"]/100 
        else "")
in
    Custom

Revised to limit to one "and":
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    ChangeType = Table.TransformColumnTypes(Source,{{" Qty1", type number}, {" PrezPub", type number}, {" CodIva", type number}}),
    Custom = Table.AddColumn(ChangeType, "Custom", each 
        if {[#" Qty1"], [#" Dove"], [Aifa]} = {1,"P","S"} then 
            Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000))-(([#" PrezPub"]*0.6/100)),2) 
        else if {[#" Qty1"],[#" Dove"]} = {1,"P"} and [Aifa]<>"S" then 
            Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000)),2) 
        else if {[#" Qty1"],[#" Dove"]} = {1,"N"} and [Aifa]<>"S" then 
            Number.Round(([#" PrezPub"]/(1+[#" CodIva"]/100))*(1-([#" Val1"]/10000)),2) 
        else if [#" Qty1"]=1 and [#" Dove"]="D" then 
            [#" PrezPub"]/100 
        else "")
in
    Custom
aFilterR1.xlsm
ABCDEFGHIJKLMNOPQRSTUVW
1First querySecond query
2
3 Qty1 DoveAifa PrezPub Val1 CodIva Qty1 DoveAifa PrezPub Val1 CodIvaCustom Qty1 DoveAifa PrezPub Val1 CodIvaCustom
41PS101000011PS10100001-0.061PS10100001-0.06
51PT10500011PT105000151PT10500015
61NT10500011NT10500014.951NT10500014.95
71DT101000011DT101000010.11DT101000010.1
82DT101000012DT101000012DT10100001
9
Sheet2
 
Upvote 0
Thank you very much for your answer.
If I paste the second formula I receive an alert message:
Formula.Firewall: Query 'Pivot best price' (step 'add customized column') refers to other query, so it can't directly access to the data origin. You have to correct your data.

Could you explain me the way to solve the problem? Perhaps because in your formula you wrote "Table 3"?

Thanks
 
Upvote 0
Yes - you need to change line 1 to retrieve the data from whatever source you are using. The way it is currently written it is retrieving from the active excel spreadsheet from a table called Table3. Replace that line with your first step retrieval code.
 
Upvote 0
Sorry but I can't solve the problem.
The source of the data is this file:
C:\Users\marco.restuccia\Desktop\Analisi_competizione\Listini.csv
This file has only one sheet named Listini.
Could you help me with the first part of the formula.

Thank you very much
 
Upvote 0
maybe

Power Query:
let
    Source = Csv.Document(File.Contents("C:\Users\marco.restuccia\Desktop\Analisi_competizione\Listini.csv"),[Delimiter=",", Encoding=65001, QuoteStyle=QuoteStyle.None]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    ChangeType = Table.TransformColumnTypes(#"Promoted Headers",{{" Qty1", type number}, {" PrezPub", type number}, {" Val1", type number}, {" CodIva", type number}}),
    Custom = Table.AddColumn(ChangeType, "Custom", each 
        if {[#" Qty1"], [#" Dove"], [Aifa]} = {1,"P","S"} then 
            Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000))-(([#" PrezPub"]*0.6/100)),2) 
        else if {[#" Qty1"],[#" Dove"]} = {1,"P"} and [Aifa]<>"S" then 
            Number.Round([#" PrezPub"]*(1-([#" Val1"]/10000)),2) 
        else if {[#" Qty1"],[#" Dove"]} = {1,"N"} and [Aifa]<>"S" then 
            Number.Round(([#" PrezPub"]/(1+[#" CodIva"]/100))*(1-([#" Val1"]/10000)),2) 
        else if [#" Qty1"]=1 and [#" Dove"]="D" then 
            [#" PrezPub"]/100 
        else "")
in
    Custom
 
Upvote 0

Forum statistics

Threads
1,223,699
Messages
6,173,903
Members
452,536
Latest member
Chiz511

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