How to calculate time difference in Power BI - powerbi

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

Related

Filter only to show durtion

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())

How to add a repeating Index or Rank column in Power Query (1,1,2,2,3,3,4,4 etc.)

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"

Index Column With Exception - POWER BI / DAX / M

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]
)

How to create multiple columns at once which depends on each other in Power BI?

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:

How to remove leading YYYY-MM-DD and format the data type in powerbi

I am trying to format the column to data type "duration" in PowerBI. In order to do this, I need to remove the YYYY-MM-DD which appeared after the data load. Im guessing that the inconsistencies are due to some values being in this format: -3:34 and orders in this: 10:43:00
This is how the column looks in powerbi:
This is how it looks in Excel before the import to powerbi:
I have tried to "replace values" in powerquery, which gives me this result:
Once I change the datatype to "duration" and "apply and load" it gives me this result:
I am expecting the result to be -02:03, 03:01. Negative values indicate an early delivery and positive values indicate a late delivery. e.g. 2h 3 min early or 3h 1 min late. Column needs to be in some form of time data type since I am planning to do calculations etc.
Any suggestions on how to solve this? Some form of table.replace()? I tried table.replace but got errors. the dax function Substitute works but I want to be able to delete one of the columns since the substitute will give me a new column.
I have done a test with a column in Excel (which is time). I imported in Power-Bi using the following query language (Please note the type duration in last step):
let
Source = Excel.Workbook(File.Contents("C:\...\Test.xlsx"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"HHMM_DELAYED", type duration}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Seconds", each Duration.TotalSeconds([HHMM_DELAYED]), type number),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "DurationText", each Duration.ToText([HHMM_DELAYED]))
in
#"Added Custom1"
This worked perfecly fine for me, see result below: