I have a column with unique Values of Countries, I just need to to create a index column.
The problem is that in this column I have a "country" that is "Other", I need this country to appears at the end.
let
Source = Table.SelectColumns(Orders,"Country"),
#"Removed Duplicates" = Table.Distinct(Source),
#"Add Index" = Table.AddIndexColumn(#"Removed Duplicates", "CountryId", 1, 1, Int64.Type)
in
#"Add Index"
"Add index but when/with Country="Other" put at the final.
Thanks you
The simplest way I can think of would be to add a new column that replaces the index for "Other" (and then delete the old index column).
Table.AddColumn(#"Add Index", "NewIndex", each
if [Country] = "Other" then List.Max(#"Add Index"[CountryId]) + 1 else [CountryId]
)
Related
I have a table with a column having timestamp as it's value. I need to add a new column & calculate the time difference between 2 rows.
How to do this?
I'm new to this & want to know how to do it please
Bring your data into powerquery, like using data... from table/range
Add column, index column
Add column, custom column, formula
= try [Timestamp] - #"Added Index"{[Index]-1}[Timestamp] otherwise null
to be fancy manually edit code to end with null, type duration)
#"Added Index" = Table.AddIndexColumn(#"PriorStepNameHere", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each try [Timestamp] - #"Added Index"{[Index]-1}[Timestamp] otherwise null, type duration)
in #"Added Custom"
Since you are new to this, I will try to help you out as much as I can, feel free to ask any question.
You can to go into power query (transform data) and create an index column this will give each datetime as unique identifier.
Create a custom column using this code
TimeDiff = if('Sheet1'[Index] <> 1, Sheet1[Timestamp] - LOOKUPVALUE ( Sheet1[Timestamp], Sheet1[Index], Sheet1[Index] - 1 ))
In format, select h:nn:ss and this will display you difference
Time
Value
10/3/2022 18:21:40
correct
10/3/2022 18:22:50
incorrect
10/3/2022 18:28:00
correct
10/3/2022 18:34:00
incorrect
From the above table, I want only filter out and show on the table if the time difference between "correct" and "incorrect" is > 5 minutes
This assumes there is always only two alternating rows of correct and incorrect, and returns a column showing the duration in minutes between them. You can then filter that, since you were vague on how and what to filter
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type datetime}, {"Value", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
#"Added Custom1" = Table.AddColumn(#"Added Index", "MinuteDuration", each if [Value] = "incorrect" then null else Duration.TotalMinutes(#"Added Index"{[Index]+1}[Time]-[Time]),type number),
#"Filled Down" = Table.FillDown(#"Added Custom1",{"MinuteDuration"})
in #"Filled Down"
Here's one way to solve your problem.
Make sure your date data is sorted chronologically ascending. (this can be done in Power Query.
Create an Index Column in Power Query
Close Power Query and add the following calculated column to your table
IfNextRowOverFiveMins =
var currentRow = 'Table (2)'[index] --this should be the index on your rable
var nextTime =LOOKUPVALUE('Table (2)'[time],'Table (2)'[index],currentRow+1) --this function looks up the time for the next entry
var timeDiff = DATEDIFF('Table (2)'[time],nextTime,MINUTE) --the datediff function finds the difference in days/hours/minutes between two date values
return IF(timeDiff>=5,TRUE(),FALSE())
Please find below links to the Fact table and the overview of all tables. I would like to create a DAX measure, or new column in the Fact table ("transactions"), where:
The currency is NOT equal to EUR (e.g. "BTC"),
Giving the value equal to a opposite value of "amount", as given in the row with EUR as currency (e.g. +5. Positive for negative and vice versa),
Where the date and time of the two rows (EUR and non-EUR) have the same values (e.g. 03/11/2021 and 12:28:06)
The "type" = "trade",
In all other cases, I think it would be best to give a value of 0.
In my Fact table screenshot, I manually added the EUR_amt column in Excel to show what I would like to create
I think it's also possible to add the column, then group by time and date, such that the rows with EUR as currency with EUR_amt being 0, would be removed. All using power query. That would be even better.
(The "Currencies" table just uses the distinct values of the "currency" column in the "transactions" table, via PowerQuery. Not relevant for this question I think)
Many thanks in advance!
-YK
Fact table "transactions"
Overview of tables
Calculated Column:
EUR_amt =
IF (
OR ( transactions[type] <> "trade", transactions[currency] = "EUR" ),
0,
- LOOKUPVALUE (
transactions[amount],
transactions[Date], transactions[Date],
transactions[Time], transactions[Time],
transactions[currency], "EUR",
0
)
)
Here's one way to do this using just Power Query and the Advanced Editor
Group by data and time
Generate a custom column for each subtable based on your rules
Expand the subtables, remove those with "0", and re-order the columns
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"type", type text}, {"currency", type text}, {"Date", type date}, {"Time", type time}, {"amount", type number}}),
//Group by Date and Time
#"Grouped Rows" = Table.Group(#"Changed Type", {"Date", "Time"}, {
//Add the Eur amt column to the grouped table
{"EUR_amt", (t)=>let
//determine relevant euro amt for each sub table
eur= t[amount]{List.PositionOf(t[currency],"EUR")},
//add the column to each subtable basaed on conditions
addCol = Table.AddColumn(t, "EUR_amt",
each if [type]= "trade" and [currency]<>"EUR" then -eur else 0)
in
addCol}}),
//Expand the new table
//Filter out the 0's
//reorder the columns
#"Expanded EUR_amt" = Table.ExpandTableColumn(#"Grouped Rows", "EUR_amt", {"type", "currency", "amount", "EUR_amt"}, {"type", "currency", "amount", "EUR_amt"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded EUR_amt", each ([EUR_amt] <> 0)),
#"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"type", "currency", "Date", "Time", "amount", "EUR_amt"})
in
#"Reordered Columns"
I have a large table of data (the below image () is a small subsection of the larger dataset) which I need to essentially add an index or rank column that repeats itself (1,1,2,2,3,3,4,4 etc. ). The reason for this is each two rows of data need to be associated with each other so I can properly pivot the attribute and value columns.
EDIT: Picture display
Bring the data into PowerQuery with data .. from table/range...
Add column ... index column ....
click select the index column
transform .. standard .. integer divide ... 2
Sample code if source data is in Table1
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"a", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 2), Int64.Type}})
in #"Integer-Divided Column"
I am new in Power BI, have a table in excel. I need to create the same in Power BI.
I have one table in Excel which has the following fields:
After first sorting the table based on ID and Date, and then adding Column A, Column B and Column C, where Column A value is equal to previous value of Column C and Column C value is equal to sum of Column A and Column B. The desired table would be:
where,
Column A = IF($A2=$A1,$G1,0)
Column B = D2-E2
Column C = SUM(E2:F2)
How to achieve this in Power BI?
I believe this may be something like what you are looking for. It requires an index so that you can use the index to reference the previous row's values.
Instead of calculating Column A from Column C like your formula suggested ($G1), I calculated it from [Revenue]. That's because Column C didn't exist yet, and wouldn't exist yet, because it wouldn't be calculated until Column A (E in your formula) exists.
Anyhow, if your table is named Table1 and has [ID], [Name], [Date], and [Revenue] just like in your first image, then you should be able to use this code. Otherwise, change "Table1" to whatever your table name is. Also, you'll need to replace "YourPathAndFile.xls" below with your path and file name--something like "C:\Users\yourname\somefolder\filename.xls". Include the quotes for both the table name and the path and file, just like below.
let
Source = Excel.Workbook(File.Contents("YourPathAndFile.xls"), null, true),
Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"ID", type text}, {"Name", type text}, {"Date", type date}, {"Revenue", Currency.Type}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"ID", Order.Ascending}, {"Date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1, Int64.Type),
#"Added Custom" = Table.AddColumn(#"Added Index", "Column A", each if [Index] = 0 then 0 else if [ID] = #"Added Index"{[Index]-1}[ID] then #"Added Index"{[Index]-1}[Revenue] else 0),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom B", each [Revenue] - [Column A]),
#"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom C", each [Column A]+[Custom B]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom2",{"Index"})
in
#"Removed Columns"
What I did was:
Started Power BI
Got the data from the spreadsheet and added the table (Table1) to Power Query (Get Data > All > Excel > Connect > Navigated to the excel file > Double-clicked the file (Could have selected the file and clicked Open instead) > Selected the relevant table (Table1) > Transform Data)
I made sure the columns' data types were what I wanted. (Select a column > Transform > Data Type: > select appropriate type > repeat for each column necessary)
I sorted the ID and Date columns (Select [ID] > click drop-down arrow > click Sort Ascending > repeat for [Date])
I added an index column (Add Column > Index Column)
I added Column A (Add Column > Custom Column > fill in as below and click OK)
I added Column B (Add Column > Custom Column > fill in as below and click OK)
I added Column C (Add Column > Custom Column > fill in as below and click OK)
I removed the Index column (Right-click [Index] > Remove)
The result is: