简体   繁体   中英

Adding a value to an existing column in Power Query Excel

How do I add a value "UTU" to the current column "List of Depot" with this logic: If there is the value "UTU" in the List of Depot column show it in the List of Depot but if there is no "UTU" in the List of Depot then still show "UTU" with value zero.

I am using power query to connect to the data source and append query to append the multiple queries together, then I connect the pivot table in Excel to the Append query to build the report. How do I go about this, I have tried conditional column but without success.

Current Output:

在此处输入图像描述

Desired Output:

在此处输入图像描述

You merely need to add a conditional clause at the end of your M code.

This has to be done in the Advanced Editor, and you'll need to extend the added record that I show to cover all of your columns.

M Code

let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{
        {"Depot", type text}, {"Report 1", Int64.Type}, {"Report 2", Int64.Type}}),

    //if number of columns is stable, this will work OK
    //if the number of columns might vary, or be named differently, we will have to make some changes    
    utuRow = if List.Contains(#"Changed Type"[Depot],"UTU")
                then #"Changed Type"
                else Table.InsertRows(#"Changed Type",
                                Table.RowCount(#"Changed Type"),
                                {[Depot="UTU",Report 1=0,Report 2=0]})
in
    utuRow

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM