I have data like this and I want to group on rows with substraction between dates
Customer Date price
Jane 01/01/2018 10
Jane 01/02/2018 14
Joe 01/01/2018 10
Joe 01/02/2018 15
I need to obtain:
Customer price
Jane 4
Joe 5
How to perform this in power Bi ?
Try adding this into a calculated column:
Difference =
var LatestDate = Table[Date]
var LatestValue = Table[Price]
var PreviousDate = Dateadd(Table[Date],-1, day)
var PreviousValue = CALCULATE(FIRSTNONBLANK(Table[Price],1),
FILTER(Table, Table[Date]=PreviousDate))
RETURN IF(CONTAINS(Table,Table[Date],PreviousDate), LatestValue-PreviousValue , 0)
Related
I am new to PowerBI and I am trying to group by the max date within the quarter, and then by unique identifier. Is this possible? My dataset looks like:
Date
CompanyID
Sales
3/31/2018
1
100
3/31/2018
2
200
3/31/2018
3
100
6/30/2018
2
300
3/31/2018
4
100
2/28/2018
4
75
1/31/2018
4
50
6/30/2018
4
200
I'm hoping to get:
Date
CompanyID
Sales
3/31/2018
1
100
3/31/2018
2
200
3/31/2018
3
100
3/31/2018
4
100
6/30/2018
2
300
6/30/2018
4
200
Appreciate any help here! Thank you!
In Power Query (Home=>Transform)
Add extra columns for Quarter and Year
Group by ID / Quarter / Year
Extract the maximum date from each subtable, along with the associated Sales number
let
//Change code in next line to reflect however you are reading in your data,
// or refer to the table you already have
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"CompanyID", Int64.Type}, {"Sales", Currency.Type}}),
//add custom columns for Quarter and Year
#"Added Custom" = Table.AddColumn(#"Changed Type", "Quarter", each Date.QuarterOfYear([Date]),Int64.Type),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Year", each Date.Year([Date])),
//Group by ID / Quarter / Year
//Then return the last date for each subtable, and the corresponding Sales figure
#"Grouped Rows" = Table.Group(#"Added Custom1", {"CompanyID", "Quarter", "Year"}, {
{"Date", each List.Max([Date]), type date},
{"Sales", (t)=>Table.SelectRows(t, each [Date] = List.Max(t[Date]))[Sales]{0}, Currency.Type}
}),
//Remove the Quarter and Year Column
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Quarter", "Year"}),
//Set the desired order
#"Reordered Columns" = Table.ReorderColumns(#"Removed Columns",{"Date", "CompanyID", "Sales"}),
//Sort by Date and Company ID Ascending
#"Sorted Rows" = Table.Sort(#"Reordered Columns",{
{"Date", Order.Ascending},
{"CompanyID", Order.Ascending}
})
in
#"Sorted Rows"
I am stuck with a problem where I have two tables, one at the months and one at the weeks. Here's the format of the tables:
Table1
Customer Date1 Sales
1 Jan2018 1110
1 Feb2018 1245
1 Mar2018 1320
1 Apr2018 1100
...
Table2
Customer Date2
1 01Jan2018
1 08Jan2018
1 15Jan2018
1 22Jan2018
1 29Jan2018
1 05Feb2018
1 12Feb2018
1 19Feb2018
1 26Feb2018
1 05Mar2018
...
I want to create a new column for sales in Table2 that will hold the disaggregated values of sales from Table1. I want to divide the sales by the number of days in that month and then assign the values to the weeks accordingly. Thus the sales in week 01Jan2018 is (1110/31)*7. The weeks that are in transition will get values from both the months. For example 29Jan2018 has 3 days in Jan2018 and 4 days in Feb2018. The sales of one day in Jan2018 is 1110/31 and the sales of one day in Feb2018 is 1245/28.
So the sales in week 29Jan2018 will be 3*(1110/31) + 4*(1245/28)
I want to do this for each distinct customer.
The resulting table should be
Result Table
Customer Date Sales
1 01Jan2018 250.6 i.e (1110/31)*7
1 08Jan2018 250.6
1 15Jan2018 250.6
1 22Jan2018 250.6
1 29Jan2018 282.27
1 05Feb2018 311.25
1 12Feb2018 311.25
1 19Feb2018 311.25
1 26Feb2018 133.39 + 170.32
Thanks!
In DATA Step programming you will be needing some 'FORWARD' data instead of some 'LAG' data. A forward value can be emulated by creating a view to the same data starting one observation forward (obs=2). After understanding the renaming semantics, it is only a matter of some easy 'bookkeeping'.
data customer_months;
attrib Customer length=8 Date1 informat=monyy. format=monyy7.; input
Customer Date1 Sales; datalines;
1 Jan2018 1110
1 Feb2018 1245
1 Mar2018 1320
1 Apr2018 1100
run;
* week data, also with computation for month the week is in;
data customer_weeks;
attrib Customer length=8 Date2 informat=date9. format=date9.; input
Customer Date2;
Date1 = intnx('month', Date2, 0);
datalines;
1 01Jan2018
1 08Jan2018
1 15Jan2018
1 22Jan2018
1 29Jan2018
1 05Feb2018
1 12Feb2018
1 19Feb2018
1 26Feb2018
1 05Mar2018
run;
* next months sales keyed on prior month value;
data customer_next_months_view / view=customer_next_months_view;
set customer_months;
Date1 = intnx('month',Date1,-1); * the month this record will be a forward for;
rename Sales=Sales_next_month;
if _n_ > 1;
run;
* merge original and forward data, rename for making clear the variable roles;
data combined;
length disag_sales 8;
merge
customer_months (rename=Sales=Sales_this_month)
customer_next_months_view
customer_weeks
;
by Date1;
days_in_this_month = intck('day',intnx('month',Date1,0),intnx('month',Date1,1));
days_in_next_month = intck('day',intnx('month',Date1,1),intnx('month',Date1,2));
day_rate_this_month = Sales_this_month / days_in_this_month;
day_rate_next_month = Sales_next_month / days_in_next_month;
if Date2 then
if month(Date2) = month(Date2+6) then
week_days_this_month = 7;
else
week_days_this_month = intck('day', Date2, intnx('month', Date2, 1));
week_days_next_month = 7 - week_days_this_month;
dollars_this_week_this_month = week_days_this_month * day_rate_this_month;
dollars_this_week_next_month = week_days_next_month * day_rate_next_month;
* desired estimated disaggregated sales;
disag_sales = sum (dollars_this_week_this_month,dollars_this_week_next_month);
run;
I have a table with following data:
Id Name Value
1 John 100
2 John -500
3 John 500
4 Smith 10
5 Smith 20
6 Smith -20
7 Stuart -10
8 Wills 25
I am looking for an efficient TSQL query which can remove John -500 and Smith -20 (i.e. records with negative value if they have a similar positive value in the same group [group by names]).
I think this is what you need. (SQL DEMO)
delete y
from mytable y join (
select id,name, value
from mytable x
where value > 0) z on y.name = z.name and y.value = -1 * z.value
select * from mytable
--SELECT RESULTS AFTER DELETING
ID NAME VALUE
1 John 100
3 John 500
4 Smith 10
5 Smith 20
7 Stuart -10
8 Wills 25
delete a
from mytable a
join mytable b
on b.name = a.name
and a.value < 0
and b.value = -1 * z.value
Almost the same as Kaf +1
Users:
userid name email
1 venkat v#g.com
2 venu ve#g.com
3 raghu r#g.com
Partners:
id userid partnerid status
1 1 2 1
2 1 3 1
location:
id userid lat lon
1 1 12.00 13.00
2 2 14.00 12.00
3 3 14.00 14.23
Query:
var result = from partner in Partners
join user in Users on partner.UserId equals user.PartnerId
join location in Locations on patner.UserId equals location.PartnerId
where partner.UserId == 1
select new { PartnerId = partner.PartnerId, PartnerName = user.Name, Lat = location.Lat, Lon = location.Lon };
by passing userid=1 as parameter I am getting this result:
partnerid patnername lat lon
2 venkat 14.00 12.00
3 venkat 14.00 14.23
by observation of above result here partnernames are wrong for partnerid = 2 - patname was venu but displaying "venkat"
For partnerid = 3, partnername was raghu but displaying venkat.
How to display the correct partner names?
I believe this JOIN here is wrong:
var result = from partner in Partners
join user in Users on user.UserId equals partner.PartnerId
You're joining a user on his userId to a partner using his PartnerID.
Don't you need to join a user to a partner using PartnerID in both cases? Something like this:
var result = from partner in Partners
join user in Users on user.PartnerId equals partner.PartnerId
i have a sql server table with records (that isn't new)
it has 1 or more records (measurements) for a person, and all records have a datetime field.
So for person 1 i have 5 records / measurements, dated jan 1, jan 2, feb 8, march 19 and july 2.
i have maybe 10 records for person 2, and the dates are different too.
Now i want to select for both person 1 and person 2 the latest record; for both persons 1 record, with the newewst datetime in the datetime field.
Can that be done with the EF?
Kind regards,
Michel
example data
personId Date Length
1 01-13-2009 180
1 01-14-2009 190
1 01-15-2009 184
2 02-18-2009 170
2 01-17-2009 190
In this table, i would like the latest record for person 1 (= row 3) and for person 2 (=row 4)
from p in Context.WhateverYourTableNameIs
group p by p.PersonId into g
select new
{
PersonId = g.Key
MostRecentMeasurementDate = (DateTime?)
(from m in g
order by m.Date desc
select m.Date).FirstOrDefault())
}