Replace value in one column with another - excel-formula

I have some doubts, i want to replace the values in column for the values of the other one, i have my query like this
= Table.ReplaceValue(#"Renamed Columns1", each ["Alcance-Canal"], null , Replacer.ReplaceValue,{"CANAL/ES"})
however it still doesn't work, could you please assist me?
Regards

Easiest thing to do is add a custom column with formula
= if [columnname] = "something" then [columnname] else [othercolumnname]
or
= if [columnname] = "something" then "anotherthing" else [othercolumnname]

Related

Power Query - Get Data from AnalysisServices (Cube) - Filter with User-Function result

I have the following problem. I'm getting Data (Excel 365 - Power Query) from a Cube with "Get Data from Analysis Services". -> Selling Qty an Values for filtered years by week.
Every thing is fine if I use Filter, updatetime ca. 3-4 seconds:
#"Filtered Rows1" = Table.SelectRows(#"Added Items", each
(Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(2019) &"]" meta
[DisplayName = Number.ToText(2019)]
or Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(2020) &"]" meta
[DisplayName = Number.ToText(2020)]
)
Now I like to do that dynamic, so that I can get the years from a cell in excel. I use the following M-Function "fktGetNamedCellValue" for this:
let
Source = (FieldInput as text) =>
let Quelle = Excel.CurrentWorkbook(){[Name=FieldInput]}[Content],
Inhalt = Number.From(Quelle{0}[Column1])
in Inhalt
in Source
I replaced the years in the Filter-Step with the function.
The cells are named "cell_Prev_Year" and "cell_Plan_Year"
The cells in Excel formated as Numbers (and there are only Numbers in it)
The updatetime now -> endless!!!
#"Filtered Rows1" = Table.SelectRows(#"Added Items", each
(Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(fktGetNamedCellValue("cell_Prev_Year") &"]" meta
[DisplayName = Number.ToText(fktGetNamedCellValue("cell_Prev_Year"))]
or Cube.AttributeMemberId([#"Date.Year (4-4-5)"]) = "[Date].[Year 4-4-5].&["&
Number.ToText(fktGetNamedCellValue("cell_Plan_Year")) &"]" meta
[DisplayName = Number.ToText(fktGetNamedCellValue("cell_Plan_Year"))]
)
If I use a "normal" parameter with the value "2019" or "2020" everything is fine.
Only if I use the fktGetNamedCellValue it will not run correctly.
I`ed Trim an Clean the result. Formated it as Text and Number... nothing helped.
I have to use userfriendly Parameter (not set in Power Query) for this, so I hope for some help :)
Best Regards
Chris
(PS: I hope u understand my english)
I solved this problem as follows.
Since it is not an good idea to use an user-function as parameter to get data from an cube. I think this method disables query-folding or is called a lot of times in the process, I decided to use an power-query parameter.
I change this Parameter with vba by checking the worksheet_change event and calling this sub:
Sub refresh_Parameter(ParameterName As String, ParameterValue As Variant)
Dim strOldFormula As String
Dim strParametersMeta As String
strOldFormula = ThisWorkbook.Queries(ParameterName).Formula
strParametersMeta = Mid(strOldFormula, InStr(1, strOldFormula, "meta"), Len(strOldFormula))
ThisWorkbook.Queries(ParameterName).Formula = ParameterValue & " " & strParametersMeta
Debug.Print strOldFormula
Debug.Print strParametersMeta
End Sub
The parameters for the sub arte the PARAMETER Name in PowerQuery and the VALUE which should be set. For this the sub extracts the meta-data from the query-formula and combines it with the new Value.
Maybe someone needs this :)
Best regards chris

Remove specific words in rows with a conditional column

I am having a problem creating a conditional column in Power bi that finds/looks up words that begin with specific letters and then remove it for the column as showing in this example below.
Values to remove are words that begins with the letters; FCL,MON and WOD
Can anybody help me?
Thank you!
If they're all the s same length then you can write it more compactly like this:
if List.Contains({"WOD", "FCL", "MON"}, Text.Start([Input],3)) then "" else [Input]
Otherwise, you need to write each separately,
if Text.StartsWith([Input], "WOD") or
Text.StartsWith([Input], "FCL") or
Text.StartsWith([Input], "MON")
then "" else [Input]
You can use create a conditional column.
Here is a screenshot of the conditional column and the condition "begins with":
Here is the result:
Here is the M code, be carefull, when you are trying to create something with M you need to "repeat" him what you just did. For example I indicated that my file had headers in the step before the conditional column, and thus, M repeats this within the new step like this : #"Promoted Headers"
#"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
#"Added Conditional Column" = Table.AddColumn(#"Promoted Headers", "Custom", each if Text.StartsWith([Input], "FCL") then " " else if Text.StartsWith([Input], "MON") then " " else if Text.StartsWith([Input], "WOD") then " " else [Input])
in
#"Added Conditional Column"
I hope this helps

Power query recursive function: add more steps after the recursive step

Background:
I have posted a question regarding a custom function in Power Query that I found in a blog by Chris Webb which I have already got an answer to.But now I have another question related to the same custom function.
One of the amazing steps in that custom function is the recursive step at the end named "OutputTable" which calls itself using a if statement, basically making it a loop. Below is the step:
OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1) then ExpandedTable else ExpandAll(ExpandedTable, NextColumnNumber)
Question:
Now what I would like to do after this step is to be able to add more transformation on the OutputTable.
For Example, I would like to add a column with just "A" in all the rows. The syntax to do that would be AddNewColumn = Table.AddColumn(OutputTable, "Test", each "A"). But when I do this this gives me an error saying that the column "Test" already exists. But i'm sure that there is no other column with name "Test". Even if I try changing the name of the column to anything else, I get the same error.
Note: Although the actual step I want to add is not AddColumn, I think I can figure out that part If I get a solution for this.
SourceCode:
let
Source = (TableToExpand as table, optional ColumnNumber as number) =>
let
ActualColumnNumber = if (ColumnNumber=null) then 0 else ColumnNumber,
ColumnName = Table.ColumnNames(TableToExpand){ActualColumnNumber},
ColumnContents = Table.Column(TableToExpand, ColumnName),
ColumnsToExpand = List.Select(List.Distinct(List.Combine(List.Transform(ColumnContents, each if _ is table then Table.ColumnNames(_) else {}))), each (_ = "view" or _ = "viewfolder" or _ = "Attribute:name")),
NewColumnNames = List.Transform(ColumnsToExpand, each ColumnName & "." & _),
CanExpandCurrentColumn = List.Count(ColumnsToExpand)>0,
ExpandedTable = if CanExpandCurrentColumn then Table.ExpandTableColumn(TableToExpand, ColumnName, ColumnsToExpand, NewColumnNames) else TableToExpand,
NextColumnNumber = if CanExpandCurrentColumn then ActualColumnNumber else ActualColumnNumber+1,
OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1) then ExpandedTable else ExpandAll(ExpandedTable, NextColumnNumber)
in
OutputTable
in
Source
I'm guessing it's throwing the error due to the recursive nature of the function calling itself and trying to apply the new column twice, once in the innermost loop and once in the outermost loop.
Let's say we have a table with two columns Col1 and Col2 that need to be expanded. If you add the new column after the OutputTable step, you'll get:
Start: Col0, Col1, Col2
OutputTable(1): Col0, Col1.a, Col1.b, Col2
OutputTable(2): Col0, Col1.a, Col1.b, Col2.x, Col2.y, Col2.z, Test
AddNewColumn: Col0, Col1.a, Col1.b, Col2.x, Col2.y, Col2.z, Test, Test
Here are a couple of approaches to try:
1. Only try to add the column when recursion is finished.
I think you can do this by changing your OutputTable line as follows:
OutputTable = if NextColumnNumber>(Table.ColumnCount(ExpandedTable)-1)
then Table.AddColumn(ExpandedTable, "Test", each "A")
else ExpandAll(ExpandedTable, NextColumnNumber)
2. Check if the column exists before trying to add it.
AddNewColumn = if Table.HasColumns(OutputTable, "Test")
then OutputTable
else Table.AddColumn(OutputTable, "Test", each "A")

Formula/Function to determine if all cells are the same but ignoring blanks

Basically, I want to indicate "TRUE" in K2 when all the cells in E2:J2 are "YES", not "NO" and ignoring blanks.
I'm not sure if I am in the right place, but I've been on this site before to look for answers.
https://docs.google.com/spreadsheets/d/14U1LpZ-C0vqNLFh6BwOdgYMCKfqIJpmH_AhrC1m4114/edit?usp=sharing
So in E2:J2, it should come out as "FALSE" because there is a "NO"
In E3:J3, it should come out as "TRUE" because they are all "YES" and the blanks are ignored
In E4:J4, it should come out as "FALSE" because all of the blanks were ignored.
The closest I came was =IF(COUNTIFS(E2:J2,">""",E2:J2,"NO")=0,,(COUNTIFS(E2:J2,">""",E2:J2,"NO")))
The problem with that function is that set it so that all blanks were treated as zero. So in the table below, K would have read "TRUE" if they were all blanks.
I'm not exactly proficient at this and everything I've done to this point has been self-taught through Googling what other people have done. I'm sure there is a simple way and I am making this way more complex than necessary. But I can't figure it out.
Any help would be greatly appreciated!
Not the most attractive formula but it works.
=IF(OR(E2 <> "",F2 <> "",G2 <> "",H2 <> "",I2 <> "",J2 <> ""),IF(AND(OR(E2 = "Yes",E2 = ""),OR(F2 = "Yes",F2 = ""),OR(G2 = "Yes",G2 = ""),OR(H2 = "Yes",H2 = ""),OR(I2 = "Yes",I2 = ""),OR(J2 = "Yes",J2 = "")),"TRUE","FALSE"),"FALSE")
EDIT - A much better formula suggested by someone on the product forms.
=ISODD((COUNTIF(E2:J2,"YES")>0)*(COUNTIF(E2:J2,"NO")=0))

Filtering a Pivot Table Automatically in Excel

I'm building a DB for school activities in the afternoon. I'm trying to create a search option through a form that controls a pivot table in which the user can filter on the class type and/or the age group and or the school year.
I wrote this code in VBA and it's not working. When i tried to write a code to filter on only one of the above (such as class type) it worked but when I expanded it to 3 filter options it isn't working. It fails when no value is inserted in one of the options.
search_class.Hide
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").ClearAllFilters
If IsNull(Range("type_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("class type").CurrentPage = Range("type_search").Value
End If
type_box = "pick a class type"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").ClearAllFilters
If IsNull(Range("target_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = "(All)"
Else: ActiveSheet.PivotTables("PivotSearchClass").PivotFields("group age").CurrentPage = Range("target_search").Value
End If
target_box = "pick a group age"
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").ClearAllFilters
If IsNull(Range("year_search").Value) Then
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = "(All)"
Else:
ActiveSheet.PivotTables("PivotSearchClass").PivotFields("school year").CurrentPage = Range("year_search").Value
End If
year_search_box = "pick a school year"
ActiveSheet.PivotTables("PivotSearchClass").PivotCache.Refresh
Does anyone know ehat the problem is and how to fix it?
I think it will work if you change your tests to either:
If IsEmpty(Range("type_search").Value)
or
If Range("type_search").Value = ""
IsNull is used to test whether a variant variable contains a null value, which won't be true with either a blank or filled cell.
I don't have Excel on this computer and pivot tables are my weakness but I'll try to help since you seem alone on this. First make sure your script is indeed running the "True" case of your if statement and then I would edit the value of CurrentPage to see if your issue is there (i.e., change the "(all)" value to something else). Where does the error lie and what is the error description?

Resources