is it possible to do a text.contain with multiple columns - powerbi

If i have a code like this
reference = List.Buffer(#"table1"[Keywords]),
Custom1 = #"Renamed Columns3",
#"Added Custom1" = Table.AddColumn(Custom1, "Custom", each [ccenter] & " " & [vnumber] & " " & [Line Description]),
#"Added Custom" = Table.AddColumn(#"Added Custom1", "Taxonomy Reference", each List.Select(
reference,
(Taxonomy_Reference)=> Text.Contains([Custom],Taxonomy_Reference))),
#"Expanded Vendors" = Table.ExpandListColumn(#"Added Custom", "Taxonomy Reference"),
What this code is doing is from table1, it's making a list out of a column and then in added custom - its doing a text.contain comparison between keywords column of table 1 with the custom column of table 2. This is working fine - however i want to refine it further by doing a comparison between vnumber and ccenter (common in both tables) first and only if ccenter and vnumber are same then do a keyword and line description comparison for those vnumbers and ccenters.

Related

Create duplicate rows in Power BI table using Power Query using date columns

I have a Power BI table sourced from blob storage. It has below structure
Here, I am constructing a composite column called "CustomKey" so that I can establish a relationship with another table.
MID
SIP
ValidFrom
ValidTo
CustomKey
1
c2
08/01/2022
11/30/2022
1_c2_08_2022
2
c3
12/24/2022
12/31/2022
2_c3_12_2022
Since the source has validFrom & ValidTo that can span across multiple months, I would like to have my table to like this so that mappings are possible from other table which has this customKey for 09/10/11 months of 2022.
MID
SIP
ValidFrom
ValidTo
CustomKey
1
c2
08/01/2022
11/30/2022
1_c2_08_2022
1
c2
08/01/2022
11/30/2022
1_c2_09_2022
1
c2
08/01/2022
11/30/2022
1_c2_10_2022
1
c2
08/01/2022
11/30/2022
1_c2_11_2022
2
c3
12/24/2022
12/31/2022
2_c3_12_2022
Can anyone guide me on how to accomplish this in Power Query?
I did try List.Generate() by passing in the ValidFrom & ValidTo & then tried to expand the list but it fails stating "Expression.Error: We cannot apply field access to the type Date."
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"MID", Int64.Type}, {"SIP", type text}, {"ValidFrom", type date}, {"ValidTo", type date}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Generate(() => Date.Year([ValidFrom])*12 + Date.Month([ValidFrom]), let End_Month = Date.Year([ValidTo])*12 + Date.Month([ValidTo]) in each _ <= End_Month, each _ + 1)),
#"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
#"Added Custom1" = Table.AddColumn(#"Expanded Custom", "Month", each if Number.Mod([Custom],12) = 0 then 12 else Number.Mod([Custom],12)),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Year", each if Number.Mod([Custom],12) = 0 then Number.IntegerDivide([Custom],12) - 1 else Number.IntegerDivide([Custom],12)),
#"Added Custom3" = Table.AddColumn(#"Added Custom2", "CustomKey", each Number.ToText([MID])&"_"&[SIP]&"_"&Number.ToText([Month])&"_"&Number.ToText([Year]))
in
#"Added Custom3"
The Power Query formulas will help you get the correct output/result.

Powerquery extract values from list in column B based on another columnA value

I have two columns A and B
A is a normal column. Let's say it may have any of the following colors: black, white, orange
B each B record contain a list. Let's say "white shirt", "white trousers", "orange t-shirt"
I'm trying to get in column B the items related to the color in column A.
If A = white, then I want in one cell "white shirt" and "white trousers".
If I hard code "white", it works, but I can't pass [A] to Text.Contains (or I don't know how)
= Table.TransformColumns(#"Added Custom", {"B", each Text.Combine(List.Transform(List.Select(_,each Text.Contains(_,"white")), Text.From), "#(lf)"), type text})
Please, I appreciate any help.
The trick is to set value of the first column equal to a variable before starting to work with the list in the second column
so for your example
= Table.TransformColumns(#"Added Custom", {"B", each let search = [ColumnA] in Text.Combine(List.Transform(List.Select(_,each Text.Contains(_,search)), Text.From), "#(lf)"), type text})
but in a more general example:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each
let search = [Column1] in
Text.Combine(
List.Select(
Text.Split([Column2],","),
each Text.Contains(_,search)
)
," ,")
)
in #"Added Custom"

QoQ Calculation Power BI

I'm calculating QoQ Imp and QoQ %Eng in the below data table which is grouped by with the help of power query by adding "Index starting from 0" and "Index.1 starting from 1".
I have a "filter" column in the Filters pane this visual. Please help me in calculating QoQ Imp and QoQ %Eng in the above Table A. The expected result/output should look like this below table:-
In Power Query (M Code), assuming your data is representative, you can compute the QoQ values before filtering/grouping etc.
Add your Index column as you show
Then add a Modulo column (value = 4, since you have four quarters)
Then add custom columns for your two calculations:
Then filter as needed
Here is the M Code assuming the data source is an Excel Table. Modify the Source line as needed
let
Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Qtr", type text}, {"Filter", type text}, {"Imp", Int64.Type}, {"Eng", Int64.Type}}),
//Compute % Eng column
#"Added Custom" = Table.AddColumn(#"Changed Type", "% Eng", each [Eng]/[Imp], Percentage.Type),
//Add Index and Modulo columns
#"Added Index" = Table.AddIndexColumn(#"Added Custom", "Index", 0, 1, Int64.Type),
#"Inserted Modulo" = Table.AddColumn(#"Added Index", "Modulo", each Number.Mod([Index], 4), type number),
//Compute QOQs -- (current row - previous row)/previous row (unless first row in the group in which case => null
#"Added Custom1" = Table.AddColumn(#"Inserted Modulo", "QoQ Imp", each if [Modulo]=0 then null
else ([Imp] - #"Inserted Modulo"[Imp]{[Index]-1}) / #"Inserted Modulo"[Imp]{[Index]-1}),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "QoQ %Eng", each if [Modulo]=0 then null
else ([#"% Eng"] - #"Inserted Modulo"[#"% Eng"]{[Index]-1}) / #"Inserted Modulo"[#"% Eng"]{[Index]-1}),
//remove now superfluous Index and Modulo columns and re-order the others
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index", "Modulo"}),
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Qtr", "Filter", "Imp", "QoQ Imp", "Eng", "% Eng", "QoQ %Eng"})
in
#"Reordered Columns"

Group by a SUM function in M power BI

In PowerQuery I try to sum the quantité livréeby BL 2 (i have several rows with the same BL2 number but i can't delete doublons, regarding of details of the data)
The data looks like:
I tried:
=Table.Group(#"Somme quantité livrée", {"BL 2"},{{"quantité livrée", each List.Sum([#"quantitée livrée"]), type number}}
but the function doesnt work, have the same error message "RightParen token excepted" but i don't see what should i do here (or even if its the right function to do what i except)
Basically i want to obtain the sum of the quantité livrée, quantité retournée, quantité facturée by distinct BL 2
Any idea?
I tried the Group By table proposed in answers but using it i lost others columns:
before:
And after:
Why not use the group interface to create the code for you?
#"Grouped Rows" = Table.Group(#"previous_step_name", {"BL 2"}, {{"quantité livrée", each List.Sum([quantité livrée]), type number}, {"quantité retournée", each List.Sum([quantité retournée]), type number}, {"quantité facturée", each List.Sum([quantité facturé]), type number}})
== == ==
If you want to retain other columns then in the group use an All Rows operation.
then after, expand desired columns back into table using arrows on top and slightly to right of the new column
== == ==
a totally different way to do this is just adding in three custom columns to sum ql, qr and qf based on BL2 of each row. It does NOT do any grouping, so for each BL combination you'd see the same total on each row
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Custom" = Table.AddColumn(Source,"sum_ql",(i)=>List.Sum(Table.SelectRows(Source, each [bl 2]=i[bl 2]) [ql]), type number ),
#"Added Custom2"= Table.AddColumn(#"Added Custom" ,"sum_qr",(i)=>List.Sum(Table.SelectRows(#"Added Custom" , each [bl 2]=i[bl 2]) [qr]), type number ),
#"Added Custom3"= Table.AddColumn(#"Added Custom2" ,"sum_qf",(i)=>List.Sum(Table.SelectRows(#"Added Custom" , each [bl 2]=i[bl 2]) [qf]), type number )
in #"Added Custom3"

increase date column based on conditions

I am having some issues with how to approach my query so any help would be greatly appreciated.
I have a date column that I need to increase based on two other columns values.
e.g. Date Reported column - 17/12/2018
If my Impact Column = "Urgent" and my Department = "Stores" I would need to increase my Date Reported Column to 18/12/2018
However if my Impact Column = "Standard" and my Department = "Floor" I would need to increase my Date Reported Column to 20/12/208
I would ideally like to not touch the original Date Reported Column but move this new value to another column.
So Far I have created a custom column and this is my code however it doesnt work.
AmendedDateReported = if(And(SurveyCorrectiveAction[Impact] = "Urgent", SurveyCorrectiveAction[LookUp] = "Stores"), Date.AddDays([DateReported],1),Blank ())
Thanks
Paula
Updated code, The formula seems to be pulling ok but the date part wont update:
#"Sorted Rows" = Table.Sort(Source,{{"DateReported", Order.Ascending}}),
#"Changed Type" = Table.TransformColumnTypes(#"Sorted Rows",{{"DateReported", type date}}),
#"Sorted Rows1" = Table.Sort(#"Changed Type",{{"DateReported", Order.Descending}}),
#"Added Custom" = Table.AddColumn(#"Sorted Rows1", "Date Repaired", each ""),
#"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"Date Repaired", type text}}),
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type1", "DateReported", "DateReported - Copy"),
#"Renamed Columns" = Table.RenameColumns(#"Duplicated Column",{{"DateReported - Copy", "AmendedDateReported"}}),
#"Merged Amendments" = Table.NestedJoin(#"Renamed Columns",{"Impact", "Department"},TLU_FaultTimeScales,{"Impact", "Department"},"TLU_FaultTimeScales",JoinKind.LeftOuter),
#"Expanded Amendments" = Table.ExpandTableColumn(#"Merged Amendments", "TLU_FaultTimeScales", {"Amendment Day"}, {"Amendment Day"}),
AmendedDateReported = Table.AddColumn(#"Expanded Amendments", "AmendedDateReported", each try Date.AddDays([DateReported],[Amendment Day]) otherwise [DateReported], type date)
in
#"Renamed Columns"
You could try:
AmendedDateReported =
Table.AddColumn(
#"Previous Step",
"Amended Date Reported",
each Date.AddDays(
[Date Reported],
if [Impact] = "Urgent" and [Department] = "Stores" then 1
else if [Impact] = "Standard" and [Department] = "Floor" then 3
else 0
),
type date
)
If you have several combinations of Impact / Department which have variable effect on amending the date, it would make more sense to put those in a separate table:
+----------+------------+----------------+
| Impact | Department | Amendment Days |
+----------+------------+----------------+
| Urgent | Stores | 1 |
| Standard | Floor | 3 |
+----------+------------+----------------+
You can then join this table to retrieve the amendment days:
#"Merged Amendments" = Table.NestedJoin(#"Previous Step",{"Impact", "Department"},tblAmendments,{"Impact", "Department"},"tblAmendments",JoinKind.LeftOuter),
#"Expanded Amendments" = Table.ExpandTableColumn(#"Merged Amendments", "tblAmendments", {"Amendment Days"}, {"Amendment Days"}),
AmendedDateReported = Table.AddColumn(#"Expanded Amendments", "Amended Date Reported", each try Date.AddDays([Date Reported],[Amendment Days]) otherwise [Date Reported], type date)
in
AmendedDateReported
Remember to update the final variable name after the in clause.