Creating Chart using measures in Power BI - powerquery

I need to create a pie chart to show the number of each rate. However, I am not able to put the measure into the value field.
So I have two tables.
List table
No
A1
A2
A3
B1
B2
Result table
Name
Year
No
Rate
ABC
2022
A1
Good
ABC
2022
B2
Good
ABC
2021
A2
Not Good
DEF
2022
A1
Not Good
GHI
2022
B1
Good
What I want
Name
Year
No
Rate
ABC
2021
A1
ok
ABC
2021
A2
Not good
ABC
2021
A3
ok
ABC
2021
B1
ok
ABC
2021
B2
ok
ABC
2022
A1
Good
ABC
2022
A2
ok
ABC
2022
A3
ok
ABC
2022
B1
ok
ABC
2022
B2
Good
DEF
2022
A1
Not good
DEF
2022
A2
ok
DEF
2022
A3
ok
DEF
2022
B1
ok
DEF
2022
B2
ok
GHI
2022
A1
ok
GHI
2022
A2
ok
GHI
2022
A3
ok
GHI
2022
B1
Good
GHI
2022
B2
ok
So to achieve what I want, I crossjoin the two tables.
Table = CROSSJOIN('List Table',SELECTCOLUMNS('Result Table', "Name", 'Result Table'[Name]))
And also added a key column to each table where I join name+year+clause. Then I can only choose many to many relationship.
To show ok, i created a measure where
if (HASONEVALUE('Result Table'[Rate]), min('Result Table'[Rate]) , "ok")
My process might be wrong too so any idea?

Now that I understand what you are trying to do better, I think I would do this from Power Query instead of DAX. You are going to have to jump through hoops to do the crossjoin, and composite keys, and relationships, and a measure. It will drive extra calculation for every report and will be slow if you have a lot of data.
In Power Query, it will take a little longer to load, but then perform faster in the reports and the data model will be clean and simple. Do the crossjoins to get the combination of Name, Year, and No, then merge in the rates you have. Then just replace nulls with "ok".
A crossjoin in Power Query needs a distinct column like this Table.Distinct(Table.SelectColumns(Data,"Name")). Then you add a column that is a table of distinct values Table.AddColumn(Source, "Year", each Table.Distinct(Table.SelectColumns(Data,"Year"))). When you expand this column, you have a crossjoin.
let
Source = Table.Distinct(Table.SelectColumns(Data,"Name")),
#"Added Distinct Years" = Table.AddColumn(Source, "Year", each Table.Distinct(Table.SelectColumns(Data,"Year"))),
#"Expanded Year" = Table.ExpandTableColumn(#"Added Distinct Years", "Year", {"Year"}, {"Year"}),
#"Added Distinct No" = Table.AddColumn(#"Expanded Year", "Custom", each Table.Distinct(Table.SelectColumns(Data,"No"))),
#"Expanded No" = Table.ExpandTableColumn(#"Added Distinct No", "Custom", {"No"}, {"No"}),
#"Merged with Data" = Table.NestedJoin(#"Expanded No", {"Name", "No", "Year"}, Data, {"Name", "No", "Year"}, "Data", JoinKind.LeftOuter),
#"Added Rate" = Table.ExpandTableColumn(#"Merged with Data", "Data", {"Rate"}, {"Rate"}),
#"Replaced Value" = Table.ReplaceValue(#"Added Rate",null,"ok",Replacer.ReplaceValue,{"Rate"})
in
#"Replaced Value"

Related

how to make measure that shows deviation

I have a table which shows sales depending on the source (fact and forecast).
year
week
category
sales rub
source
2021
32
shorts
54387
2021 fact
2021
32
shorts
58264
forecast
2021
33
dresses
4325
2021 fact
2021
33
dresses
5432
forecast
When I make a matrix in powerBI need to get a deviation fact from forecast, bu I cannot make a quick measure division because in fact i have only one column with values. How can i calculate the deviation? Thanks a lot
If using powerquery, load data
click select source column
transform .. pivot column ... and for values column, choose sales rub from dropdown
add column .. custom column. Name it deviation with formula
= [2021 fact]-[forecast]
full sample code:
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Pivoted Column" = Table.Pivot(Source, List.Distinct(Source[source]), "source", "sales rub", List.Sum),
#"Added Custom" = Table.AddColumn(#"Pivoted Column", "deviation", each [2021 fact]-[forecast])
in #"Added Custom"

Insert rows for missing dates in Power Query

the starting point is the following table in which entries are made for events on specific days (journal).
Entity
Event
Date
Amount
0123
acquisition
05.05.2015
10,000.00
0123
capital increase
30.11.2015
1,000.00
0123
write-off
31.12.2017
-4,000.00
0123
write-up
31.12.2019
3,000.00
This journal is loaded into Power Query to be enhanced with additional information from other sources.
The goal is a Power Pivot table in which the amounts are summarized as at 31.12. of each year (Subtotals).
Year
Entity
Event
Date
Amount
2015
0123
aquisition
05.05.2015
10,000.00
2015
0123
capital increase
30.11.2015
1,000.00
2015 Subtotal
0123
11,000.00
2016 Subtotal
0123
11,000.00
2017
0123
write-off
31.12.2017
-4,000.00
2017 Subtotal
0123
7,000.00
2018 Subtotal
0123
7,000.00
2019
0123
write-up
31.12.2019
3,000.00
2019 Subtotal
0123
10,000.00
2020 Subtotal
0123
10,000,00
The question is how to insert rows in Power Query for years where no activity (event) has occurred (no entry in the journal) so that a subtotal can be shown in Power Pivot as of 31.12. of each year.
I hope I could explain my issue in an understandable way. Thanks in advance for your help!
Kind regards,
Joerg
See if something like this works for you. There are shorter, more confusing ways to do it
Get minimum year of all the data, and maximum year of all the data, and create a table of all combinations of years and entities. See if those are being used. If not, merge that year and entity back into the original table with month=dec day=31
there is a bit of self-merging etc, which requires pasting this into home...advanced... since not all of it can be done in the user interface
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Entity", Int64.Type}, {"Event", type text}, {"Date", type date}, {"Amount", Int64.Type}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Year", each Date.Year([Date])),
// Create table of all possible Entities and Years
DateList = {Date.Year(List.Min(#"Added Custom"[Date])) .. Date.Year(List.Max(#"Added Custom"[Date]))},
Entities = Table.AddColumn(Table.Distinct(Table.SelectColumns(#"Added Custom",{"Entity"})),"Year", each DateList),
#"Expanded Year" = Table.ExpandListColumn(Entities, "Year"),
// Find unique Data and merge into original data set
#"Merged Queries" = Table.NestedJoin(#"Expanded Year",{"Year", "Entity"},#"Added Custom",{"Year", "Entity"},"Table2",JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Date"}, {"Date2"}),
#"Filtered Rows" = Table.SelectRows(#"Expanded Table2", each ([Date2] = null)),
#"Added Custom1" = Table.AddColumn(#"Filtered Rows", "Date", each #date([Year],12,31), type date),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Date2", "Year"}),
#"Appended Query" = Table.Combine({#"Changed Type", #"Removed Columns" })
in #"Appended Query"

Pick the earliest date record

I have a doubt in Oracle SQl, I have to pick the earliest record based on date ranges. I have table A with
NAME.A SHOPNO YEAR.A shop key
XX 123 2013 1
YY 345 2013 2
I have table b with two columns .
SHOPNO NUMBER.B NAME.B INSERT_DATE.B
1 987 ZZ 7/13/2013
2 456 ZZ 12/1/2013
My resulting output should be
NAME.A SHOPNO YEAR.A NUMBER.B NAME.B
XX 123 2013 987 ZZ
Please let me know how it can be achieved in oracle sql
See this answer from a duplicate question for a more efficient solution using rank(). But here's the basic/simple way to do what you want.
select a.name, a.shopno, a.year, b.number, b.name
from a
join b on a.shopno = b.shopno
and b.insert_date = (select min(insert_date) from b b2
where b2.shopno = b.shopno);

How to group by multiple columns and then transpose in Hive

I have some data that I want to group by on multiple columns, perform an aggregation function on, and then transpose into different columns using Hive.
For example, given this input
Input:
hr type value
01 a 10
01 b 20
01 c 50
01 a 30
02 c 10
02 b 90
02 a 80
I want to produce this output:
Output:
hr a_avg b_avg c_avg
01 20 20 50
02 80 90 10
Where there is one distinct column for each distinct type in my input. a_avg corresponds to the average a value for each hour.
How can I do this in Hive? I am guessing I might need to make use of https://github.com/klout/brickhouse/wiki/Collect-UDFs
So far the best I can think of is to use multiple group-by clauses, but that won't transpose the data into multiple columns.
Any ideas?
You don't necessarily need to use Brickhouse, but it will definitely make it easier. Here is what I'm thinking, something like
select hr
, type_map['a'] a_avg
, type_map['b'] b_avg
, type_map['c'] c_avg
from (
select hr
, collect(type, avg_value) type_map -- Brickhouse collect; creates a map
from (
select hr
, type
, avg( value ) avg_value
from db.table
group by hr, type ) x
group by hr ) y

Event Study (Extracting Dates in SAS)

I need to analyse abnormal returns for an event study on mergers and acquisitions.
** I would like to analyse abnormal returns to acquirers by using event windows. Basically I would like to extract the prices for the acquirers using -1 (the day before the announcement date), announcement date, and +1 (the day after the announcement date).**
I have two different datasets to extract information from.
The first is a dataset with all the merger and acquisition information that has the information in the following format:
DealNO AcquirerNO TargetNO AnnouncementDate
123 abcd Cfgg 22/12/2010
222 qwert cddfgf 26/12/1998
In addition, I have a 2nd dataset which has all the prices.
ISINnumber Date Price
abcd 21/12/2010 10
abcd 22/12/2010 11
abcd 23/12/2010 11
abcd 24/12/2010 12
qwert 20/12/1998 20
qwert 21/12/1998 20
qwert 22/12/1998 21
qwert 23/12/1998 21
qwert 24/12/1998 21
qwert 25/12/1998 22
qwert 26/12/1998 21
qwert 27/12/1998 23
ISIN number is the same as acquirer no, and that is the matching code.
In the end I would like to have a database something like this:
DealNO AcquirerNO TargetNO AnnouncementDate Acquirerprice(-1day) Acquireeprice(0day) Acquirerprice(+1day)
123 abcd Cfgg 22/12/2010 10 11 12
222 qwert cddfgf 26/12/1998 22 21 23
Do you know how I can get this?
I'd prefer to use sas to run the code, but if you are familiar with any other programs that can get the data like this, please let me know.
Thank you in advance ^_^.
This can be done quite easily with PROC SQL and joining the PRICE dataset three times. Try this (assuming data set names of ANNOUCE and PRICE):
Warning: untested code
%let day='21DEC2010'd;
proc sql;
create table RESULT as
select a.dealno,
a.acquirerno,
a.targetno,
a.annoucementdate,
p.price as acquirerprice_prev,
c.price as acquirerprice_cur,
n.price as acquirerprice_next
from ANNOUCE a
left join (select * from PRICE where date = &day-1) p on a.acquirerno = p.isinumber
left join (select * from PRICE where date = &day) c on a.acquirerno = c.isinumber
left join (select * from PRICE where date = &day+1) n on a.acquirerno = n.isinumber
;
quit;

Resources