Could you please help me?!
I have 2 tables in my desktop(see below) and I want to generate desired table based on the 2 tables I have in Power BI.
Date
EmpId
Hours
11/11/2021
100001
168
3/1/2022
100001
145
5/5/2022
100001
160
1/1/2022
100002
168
Desired Output table should look like below:
Date
EmpID
EmpName
Contracthours
1/1/2022
100001
Employee A
168
1/2/2022
100001
Employee A
168
1/3/2022
100001
Employee A
168
1/4/2022
100001
Employee A
168
1/5/2022
100001
Employee A
168
1/6/2022
100001
Employee A
168
1/7/2022
100001
Employee A
168
1/8/2022
100001
Employee A
168
1/9/2022
100001
Employee A
168
1/10/2022
100001
Employee A
168
2/28/2022
10001
A
168(till 2/28/2022)
3/1/2022
10001
A
145(Till 5/4/2022)
Any suggestion on this?
Thanks. Have a nice day.
I want to fill the dates between the dates in same column in power bi
I assume you mean to ask how to repeat the contents of the rows for every date from that row until the date on the row below it, grouping by employee
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(Source, {"EmpId"}, {{"data", each
let a=Table.AddIndexColumn(_, "Index", 0, 1, Int64.Type),
b = Table.AddColumn(a, "Custom", each try Date.AddDays(a{[Index]+1}[Date],-1) otherwise [Date]),
c= Table.AddColumn(b, "Custom.1", each {Number.From([Date]) .. Number.From([Custom])}),
d = Table.ExpandListColumn(c, "Custom.1")
in d
, type table }}),
#"Expanded data" = Table.ExpandTableColumn(#"Grouped Rows", "data", {"Hours", "Custom.1"}, {"Hours", "Custom.1"}),
#"Changed Type" = Table.TransformColumnTypes(#"Expanded data",{{"Custom.1", type date}})
in #"Changed Type"
Related
I have a rather large table in PowerBI that looks as follows:
Date1
ID1
ID2
Date2
Amount1
Amount2
Amount3
04.02.2022
1234
12
04.02.2022
5
3
8
04.02.2022
1234
13
04.02.2022
5
3
8
04.02.2022
1235
14
04.02.2022
6
3
9
06.02.2022
1234
10
06.02.2022
20
23
46
06.02.2022
1238
11
06.02.2022
20
23
46
06.02.2022
1238
14
06.02.2022
26
23
49
As in the case above, if e.g. 05.02.2022 is missing, I would like my end result to look like
Date1
ID1
ID2
Date2
Amount1
Amount2
Amount3
04.02.2022
1234
12
04.02.2022
5
3
8
04.02.2022
1234
13
04.02.2022
5
3
8
04.02.2022
1235
14
04.02.2022
6
3
9
05.02.2022
1234
12
05.02.2022
5
3
8
05.02.2022
1234
13
05.02.2022
5
3
8
05.02.2022
1235
14
05.02.2022
6
3
9
06.02.2022
1234
10
06.02.2022
20
23
46
06.02.2022
1238
11
06.02.2022
20
23
46
06.02.2022
1238
14
06.02.2022
26
23
49
Which means that everything from 04.02.2022 is copy pasted, just with a new date, 05.02.2022.
There are also cases where no data is available for 2 or 3 days, so in those instances I would need the all data from the last known date, until we have data again.
Does someone know how to implement this in PowerBI?
Thank you!
The following should work for you. I have named your sample data query as Table.
Create a new query and paste in the following code. This new query refers to your sample data query named Table so you will have two queries.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDRMzDSMzIwMlKK1QFyTVG5ZghuLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Date"}, Table, {"Date1"}, "Table", JoinKind.LeftOuter),
#"Added Custom" = Table.AddColumn(#"Merged Queries", "Count", each if Table.RowCount([Table]) > 0 then [Table] else null),
#"Filled Down" = Table.FillDown(#"Added Custom",{"Count"}),
#"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom", each if Table.RowCount([Table]) > 0 then [Table] else Table.ReplaceValue([Count],[Count]{0}[Date1],[Date],Replacer.ReplaceValue,{"Date1", "Date2"})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Table", "Count"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Date1", "ID1", "ID2", "Date2", "Amount1", "Amount2", "Amount3"}, {"Date1", "ID1", "ID2", "Date2", "Amount1", "Amount2", "Amount3"}),
#"Removed Columns1" = Table.RemoveColumns(#"Expanded Custom",{"Date"})
in
#"Removed Columns1"
If you need to fill out more dates, then just change the date range in step 1 which you should be able to auto generate depending on your data. Mine looks like this.
I want to numerate the occurances of a specific column in my table. The best way I have thought to do this is to count the rows of a filtered table. So, my WorkOrders table looks like this:
WO# Date CompCode Serial#
001 1/1/2021 100 A
001 1/1/2021 101 A
002 1/2/2021 100 B
003 2/1/2021 100 A
004 2/2/2021 100 B
005 2/15/2021 101 A
006 3/1/2021 102 A
006 3/1/2021 100 A
I want to create a new column that numerates the occurance on the CompCode by Serial#. There is no gaurantee that the data is sorted by date. So, I tried to count the rows of a filtered table using this formula:
COMP_OCCURANCE =
CALCULATE(
COUNTROWS(WorkOrders),
Serial# = Serial#,
Date <= Date,
CompCode = CompCode
)
I assumed that would work but it does not. The desired result would look like this:
WO# Date CompCode Serial# COMP_OCCURANCE
001 1/1/2021 100 A 1
001 1/1/2021 101 A 1
002 1/2/2021 100 B 1
003 2/1/2021 100 A 2
004 2/2/2021 100 B 2
005 2/15/2021 101 A 2
006 3/1/2021 102 A 1
006 3/1/2021 100 A 3
Thanks in advance for the help.
Try this:
COMP_OCCURANCE =
VAR CurrentSerial = WorkOrders[Serial #]
VAR CurrentDate = WorkOrders[Date]
VAR CurrentCode = WorkOrders[CompCode]
VAR Result =
CALCULATE (
COUNTROWS ( WorkOrders ),
WorkOrders[Serial #] = CurrentSerial,
WorkOrders[CompCode] = CurrentCode,
WorkOrders[Date] <= CurrentDate,
REMOVEFILTERS ( WorkOrders )
)
RETURN
Result
Screenshot - https://ibb.co/Bj5xrFS
For this type of calculation, you must need to rely on a column for sorting/ordering your data. As you cannot use Date in this case, I think you can go for column "WO#" this case. If this is acceptable, you can try this below measure for your expected output-
COMP_OCCURANCE =
CALCULATE(
COUNT(your_table_name[WO#]),
FILTER(
ALL(your_table_name),
your_table_name[WO#] <= MIN(your_table_name[WO#])
&& your_table_name[CompCode] = MIN(your_table_name[CompCode])
&& your_table_name[Serial#] = MIN(your_table_name[Serial#])
)
)
Output-
I'm calculating the difference of "closed column". All data is in one column and I'm calculating the difference between Row2-Row1 for all the rows. I'm getting results as some positive values and some negative. Positive values are coming correct but negative values are incorrect. I'm applying the formula
diff =
Table3[Value] -
CALCULATE(
SUM (Table3[Value]),
FILTER(
Table3,
Table3[Index] = EARLIER(Table3[Index])- 1
)
).
Screenshot of my formula
Output after applying formula, -ve and +ve values
Please help how can I correct my -ve values?
Month Week Month End Closed Open GT IN
01/2020 W01-2020 N 71 178 249 71
01/2020 W02-2020 N 284 189 473 213
01/2020 W03-2020 N 550 210 760 266
01/2020 W04-2020 N 861 185 1046 311
01/2020 W05-2020 Y 1185 205 1390 324
02/2020 W06-2020 N 370 206 576 370
02/2020 W07-2020 N 665 209 874 295
In Power Query Editor, I have added an Index column started from 1 to the data and the output is as below-
Now, create this below measure to get previous rows Closed value in the current row-
prev_row_closed_value =
CALCULATE(
SUM (your_table_name[Closed]),
FILTER(
ALL(your_table_name),
your_table_name[Index] = MIN(your_table_name[Index]) - 1
)
)
For calculating difference, use this below measure-
diff =
MIN(your_table_name[Closed]) -
CALCULATE(
SUM (your_table_name[Closed]),
FILTER(
ALL(your_table_name),
your_table_name[Index] = MIN(your_table_name[Index]) - 1
)
)
Here is output from the above measure-
In Dax you can use the following formulas.
In step one we create a column to get your Week Column in an order:
YearWeek = CONVERT(RIGHT(Sheet1[Week], 4) & MID(Sheet1[Week],2,2),INTEGER)
This is creating an integer value our of your year and month. Next we can use this to get the previous closed amount to be substracted where we filter first on the correct month. Be aware that I take the assumption this is a date column.
In =
var curMonth = Sheet1[Month]
var curYW = Sheet1[YearWeek]
var filterMonthYW = FILTER(Sheet1, curMonth = Sheet1[Month] && curYW > Sheet1[YearWeek])
var MaxYW = CALCULATE(MAX(Sheet1[YearWeek]), filterMonthYW)
return Sheet1[Closed] - CALCULATE(MAX(Sheet1[Closed]), FILTER(filterMonthYW, MaxYW = Sheet1[YearWeek] ))
Sheet1 is your table..
End result:
I have the leave data of a company.
Here is the sample data:
STAFF PL CL ML SP LWP TL Month
A 1 2 0 0 6 9 April
B 14 0 4 0 0 18 April
A 1 2 0 0 1 4 May
B 1 0 4 0 0 5 May
A 1 2 0 0 2 5 June
B 2 0 4 0 0 6 June
I want to transform this data into a table structure like this-
Here is the sample data:
Types of Leave Count Month
ML 89 4
CL 114 4
LWP 17 4
PL 135 4
SP 89 4
ML 89 5
CL 114 5
LWP 17 5
PL 135 5
SP 89 5
ML 89 6
CL 114 6
LWP 17 6
PL 135 6
SP 89 6
Can it be possible using SelectColumns, Summarizecolumn dax functions?
I tried --
SUMMARIZE(Table1, Table1[CL],Table1[LWP],Table1[ML],Table1[PL],"CL2", SUM(Table1[CL]), "ML2", SUM(Table1[ML]), "LW2P",SUM(Table1[LWP]), "P2L", SUM(Table1[PL]))
It just gave me weird results.
Unpivot the data after removing the name and the staff columns. Then convert the month name to the month number and build a pivot table.
The screenshot shows the result in Excel, but the same can be done in Power BI.
Here is the M code for the query as it was recorded when I clicked the ribbon icons:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"STAFF", type text}, {"PL", Int64.Type}, {"CL", Int64.Type}, {"ML", Int64.Type}, {"SP", Int64.Type}, {"LWP", Int64.Type}, {"TL", Int64.Type}, {"Month", type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"STAFF", "TL"}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Month"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Type of Leave"}, {"Value", "Count"}}),
#"Added Custom" = Table.AddColumn(#"Renamed Columns", "Custom", each Date.FromText([Month]&"1 2017")),
#"Extracted Month" = Table.TransformColumns(#"Added Custom",{{"Custom", Date.Month}}),
#"Removed Columns1" = Table.RemoveColumns(#"Extracted Month",{"Month"}),
#"Renamed Columns1" = Table.RenameColumns(#"Removed Columns1",{{"Custom", "Month"}})
in
#"Renamed Columns1"
I have a table in PowerBI that looks like this:
Date StoreID Car Sales <Row Num (for explanation only)>
1/1/2017 1 0 1
1/2/2017 1 0 2
1/3/2017 1 0 3
1/4/2017 1 20 4
1/5/2017 1 13 5
1/6/2017 1 0 6
1/7/2017 1 31 7
1/4/2017 2 0 8
1/5/2017 2 0 9
1/6/2017 2 7 10
1/7/2017 2 0 11
1/8/2017 2 10 12
What I am trying to do is create a measure that will calculate Car Sales by day (so on a line chart with Date on the x-axis), but eliminate the rows/records with 0's until the first Date that has a Sales value. In other words, I want to eliminate rows 1, 2, and 3, but not eliminate row #6, because that is a legitimate day where there were no cars sold. I also want to do this for every StoreID, so I want to eliminate rows 8 and 9, but not 11. Is there any way to come up with a measure/column (or other means) that will accomplish this in PowerBI?
You can group by StoreID, and then transform each column with: sort by [Date], then use Table.Skip to remove rows where [Car Sales] is 0. (Sorting by [Date] might not seem necessary, but group by can change ordering.) Then expand out the grouped tables.
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"StoreID", Int64.Type}, {"Car Sales", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"StoreID"}, {{"Grouped", (grouped) => let
#"Sorted Rows" = Table.Sort(grouped,{{"Date", Order.Ascending}}),
SkipNoCarSales = Table.Skip(#"Sorted Rows", each [Car Sales] = 0)
in
SkipNoCarSales, type table}}),
#"Expanded Grouped" = Table.ExpandTableColumn(#"Grouped Rows", "Grouped", {"Car Sales", "Date"}, {"Car Sales", "Date"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Grouped",{"Car Sales", "StoreID", "Date"})
in
#"Reordered Columns"