Creating Conditional Column in excel or power bi - excel

I have a problem which can be solved in either excel or power bi. From the image provided i want to create a new column so that the Program/Course will only contain the values Program and values containing Kurs will be in their own column. Is there a way to do that using either excel or Power BI.

I would use the Query Editor to rename the current Program/Course column, then create 2 Conditional Columns to split the data the way you want e.g.
Program/Course = if [Program/Course - Original] <> "Kurs" then [Program/Course - Original] else null
Course = if [Program/Course - Original] = "Kurs" then [Program/Course - Original] else null
Finally I would remove the renamed column.

Related

Excel 2016 query editor, filter/where clause

I'm struggling with probably the simplest thing. How do I filter my data using a where clause in Excel 2016, ODBC sql database source.
I'm new to Excel 2016, I used to just modify the SQL Command via the odbc properties. Now i can't even see the SQL command when I create the odbc.
So from just playing around. I gathered that there is now a series of "Applied Steps", but I struggle to find where I can find how to limit the rows to a date range. (I don't want to display the date because I'm aggregating by inventory type) But there must be somewhere I can say return only for date '2020-05-01' for example. (where date = '2020-05-01')
the next steps will then be making that a dynamic parameter so I can have some sort of input when the data refresh.
Any tips?
The SQL statement is greyed out
Right clicking a step -> view native SQL will gray out when that step cannot be query folded. Previous steps are still viewable.
You can find the sql query and server name if you choose advanced editor -- which displays all steps in one editor.
native sql
If you want raw sql, import the table as a new connection and paste your query into that box.
note: [] in powerquery is a Record literal.
// indented for readability
let
Source = Sql.Database("server", "dbo"),
// using parameterized queries
RawSQL = "SELECT * FROM users as
WHERE EnglishMonthName=#MonthName
AND
EnglishDayNameOfWeek=#DayName",
TableName = Value.NativeQuery(
Source, RawSQL
[
MonthName="March",
DayName="Tuesday"
]
)
in
TableName
I don't want to display the date because I'm aggregating by inventory type
Using the UI, you would import the table keeping the date column. Filter rows. Then choose remove column as your last step. Your model will not display removed columns.
Where clause in PowerQuery aka: Get Data
Where is the function Table.SelectRows The UI will generate the code for you
Right click your date column -> filter -> date/time -> equals
If you want 2020/05/09 It generates:
= Table.SelectRows(#"Source Table", each [Date] = #date(2020, 5, 9))
You may use multiple conditions, like a date range
= Table.SelectRows(#"Table Name", each
[Date] >= #date(2020, 5, 9) and
[Date] <= #date(2020, 6, 1)
)

Convert Excel formula to Power BI

Im a new user in Power BI and im having a lot of trouble trying to convert my formulas in excel to work in Power BI.
My main issue is figuring out how to convert IF(AND(OR and several IF's. I have several formulas similar to this one so with the help of converting this one I could most likely do the rest based off this one.
Parameters would be a table in power bi.
E2, Q2, $B$2, etc. would be columns/fields in different tables in power bi.
=IF(E2="";"";IF(AND(OR(E2="Critical";E2="Maximum";E2="Urgent");Q2*24>Parameters!$B$2);"NO";
IF(AND(OR(E2="Alta";E2="High");Q2*24>Parameters!$B$3);"NO";
IF(AND(OR(E2="Media";E2="Medium");Q2*24>Parameters!$B$4);"NO";
IF(AND(OR(E2="Baja";E2="Low");Q2*24>Parameters!$B$5);"NO";
"YES")))))
This formula would be in a new column inside one of the tables.
You already did the work, it's very very similar, I've just added a new column and modify your formula like this:
Column = IF(Table1[E2]="","",
IF(AND(OR(Table1[E2]="Critical",OR(Table1[E2]="Maximum",Table1[E2]="Urgent")),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Alta",Table1[E2]="High"),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Media",Table1[E2]="Medium"),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Baja",Table1[E2]="Low"),Table1[Q2]*24>Table1[B2]),"No","YES"
)
)
)
)
)
Where Table1 is my table, E2,Q2 and B2 are my columns name. About the "Q2*24", because you look like to use always Q2 and not Q2,Q3,Q4,... you can use one VAR instead of a full column with always the same value, like this:
Column =
VAR ReplaceQ2 = 10
RETURN
IF(Table1[E2]="","",
IF(AND(OR(Table1[E2]="Critical",OR(Table1[E2]="Maximum",Table1[E2]="Urgent")),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Alta",Table1[E2]="High"),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Media",Table1[E2]="Medium"),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Baja",Table1[E2]="Low"),ReplaceQ2*24>Table1[B2]),"No","YES"
)
)
)
)
)
Hope this help
=IF(AND(LEFT(L6,3)="CGK",LEFT(M6,3)="CGK"),"Intracity",IF(AND(LEFT(N6,3)="CTC",LEFT(L6,3)=LEFT(M6,3)),"Intracity",IF(AND(LEFT(L6,3)=LEFT(M6,3),LEFT(N6,3)<>"CTC"),"Intercity","Domestik")))

Cleaning Excel Table using VBA without impacting the entire table and formatting

Hi I am trying to change to write VBA for excel to clean up data elements that has extra information without impacting the other elements.
I am writing VBA for the first time my table is in the middle of the sheet.
Given Table and Requested Output.
I think your question was not clear in regard to the "steps" that you want to perform on your data (i.e. the exact logic or transformation that needs to be applied).
Based purely on your images and your comment, I make the "steps" to be:
Split any customer IDs in column valueC into multiple rows.
If column valueC does not contain customer IDs (i.e. is blank or contains non-customer ID text), leave it untouched.
My answer uses Power Query instead of VBA. If you are interested in trying it out, in Excel try clicking Data > Get Data > From Other Sources > Blank Query, then click Advanced Editor near the top-left, copy-paste the code below, then click Done.
You might need to change the name of the table in the first line of the code (below), as it was "Table1" for me, but I imagine yours is named something else. Also, the code below is case-sensitive. So if there is no column named exactly valueC, then you will get an error.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
fxProcessSomeText = (textToProcess as any) =>
let
canBeSplit = Text.StartsWith(textToProcess, "### customer id"),
result = if textToProcess is null then null else if canBeSplit then Text.Split(Text.BetweenDelimiters(textToProcess, "### customer id", " ###"), ",") else {textToProcess}
in
result,
invokeFunction = Table.TransformColumns(Source, {{"valueC", fxProcessSomeText}}),
expanded = Table.ExpandListColumn(invokeFunction, "valueC"),
reindex =
let
removeIndex = Table.RemoveColumns(expanded, {"index"}),
addIndex = Table.AddIndexColumn(removeIndex, "index", 1, 1),
moveIndex = Table.ReorderColumns(addIndex, List.Distinct(List.InsertRange(Table.ColumnNames(addIndex), 0, {"index"})))
in
moveIndex
in
reindex
My output table contains more rows than yours. Also, the value in column valueA, row 11 is 1415 for me (it is 1234 in your request output). Not sure if this is a mistake in your example, or if I'm missing some logic.

How to validate source data using a Range validation table in Excel Power query

Can anyone please help me on this?
I am trying to validate the source data in 3 columns BU - Act - Dept in the Range validation Data [BU - Beginning Act - End Account - Beginning Dept - End Dept] (screenshot attached).
BU is a single column in validation table but Act & Dept columns are range columns. I need to check if the BU - Act - Dept combo exists in Range data.
So does Power query has any functionality to validate the source data using a nested join?
This needs to be done in Excel power query and not formulas because the validation Range file has more than 1 million records.
I'm not sure how efficient this is, but it works in principle.
I'll assume you have both tables loaded into the Query Editor with names Source and Validation.
First, choose the Source query and merge in the Validation table, matching on Source[Unit] = Validation[BU] for a left outer join.
Once merged, expand all the columns except Validation[BU]. This will give you a table with more rows since it will pull over every row in Validation that corresponds to the Source[Unit].
Now you can write a validation Status column. Add Column > Custom Column:
= if [Account] >= [Beg Act] and [Account] <= [End Act] and
[Dept] >= [Beg Dept] and [Dept] <= [End Dept]
then "Valid" else "Invalid"
Now that you have this column, group by the first three columns and take the max over the new custom column, Status. This should reduce your table back to its original size and give "Valid" in the Status column if that row matched any of the conditions that were pulled over from the Validation table, otherwise "Invalid".

Convert SAP BI hierarchy to flat table

I have a table:
Old table
Level 1 is top level. CC is the smallest unit inside our company.
What I want to do is convert this table into a flat table with additional column like level 1 / level 2 / Level 3, which show parent department of each node,
e.g. 100111 |CC |3 |IS// |IS/ |IS.
New Table
Using Excel I can do it easily by using some conditional formula and copy the cell above if current cell is CC.
My process is like this: SAP Application (export)-> .xls file (without Level and Parent Columns) -> creating new column for level and parent node with power query -> make new column (level 1 - 6) like example in the new table.
For Column Level 1 i use this formula:
If(B2=1;A2;D1)
and i fill it down for the rest. In my data, the first row is always level 1.
For Level 2:
=IF(B2=2(//because is Level 2),A2,IF(B2<2,"",E1))
And i repeat the same formula for other Column.
Can someone suggest me a solution for this problem?
I think the Power Query equivalent of your first formula would be to Add a Column with this formula:
if [Level] = 1 then [Department] else null
I would follow that with a "Fill / Down" step (from the Transform ribbon).
The subsequent formulas would look similar, e.g. for Level 2
if [Level] = 2 then [Department] else null
Follow each with a "Fill / Down" step and you should be done.

Resources