MDX Measures on Rows and CurrentMember.UniqueName in Column - mdx

Need helping getting Member Full Path and Ordinality rotated on columns
I have cube with the following:
1 dimension with 4 levels (dim) in its hierarchy.
Example in table:
GCA = Group Customer A = Level 0,
EC = End Customer A = level 1
1 time dimension with Fiscal Year, Quarter and Month Level
2 measures (Revenue and Cost)
2 calculated measures (Gross Margin and Gross Margin%)
I use a LastPeriods statement on columns to display the last X quarters in column
I use a ToggleDrillState statement to drill on the dim and crossjoin this with the 4 measures.
To be able to style the table on the front end, I need the dim MemberFullPath and ordinality.
This gives me the following result:
DimensionWith4Lvl Measures 2017Q1 2017Q2
GCA Revenue 100 110
GCA Cost 60 70
GCA GM 40 40
GCA GM% 40% 36%
GCA MemberOrdinal 0 0
GCA MemberFullPath [Dim].[GCA] [Dim].[GCA]
EndCustA Revenue 100 110
EndCustA Cost 60 70
EndCustA GM 40 40
EndCustA GM% 40% 36%
EndCustA MemberOrdinal 1 1
EndCustA MemberFullPath [Dim].[GCA].[ECA] [Dim].[GCA].[ECA]
I need to get, however, the following output:
DimensionWith4Lvl Measures 2017Q1 2017Q2 MemberFullPath MemberOrdinal
GCA Revenue 100 110 [Dim].[GCA] 0
GCA Cost 60 70 [Dim].[GCA] 0
GCA GM 40 40 [Dim].[GCA] 0
GCA GM% 40% 36% [Dim].[GCA] 0
EndCustA Revenue 100 110 [Dim].[GCA].[ECA] 1
EndCustA Cost 60 70 [Dim].[GCA].[ECA] 1
EndCustA GM 40 40 [Dim].[GCA].[ECA] 1
EndCustA GM% 40% 36% [Dim].[GCA].[ECA] 1
This is my query.
with member [Measures].[Member Full Path] as '[Revenuedim].CurrentMember.UniqueName'
member [Measures].[Member Ordinal] as '[Revenuedim].CurrentMember.Ordinal'
member [Measures].[GM] as '([Measures].[Revenue_USD] - [Measures].[Cost_USD])'
member [Measures].[GM%] as 'IIF( [Measures].[Revenue_USD] > 2 , (([Measures].[GM] / [Measures].[Revenue_USD]) * 100), NULL)'
member [Measures].[Revenue] as '[Measures].[Revenue_USD]'
member [Measures].[Cost] as '[Measures].[Cost_USD]'
select LastPeriods(4, [Date.YQM].LastChild.LastChild) ON 0,
Crossjoin(
order(
Filter(
ToggleDrillState(
ToggleDrillState(
{[Revenuedim].[All GCs]}, {[Revenuedim].[All GCs] })
, {} )
, [Measures].[Revenue] <> 0 OR [Measures].[Cost] <> 0)
,[Measures].[Revenue], DESC)
, {[Measures].[Revenue],
[Measures].[Cost],
[Measures].[GM],
[Measures].[GM%],
[Measures].[Member Full Path],
[Measures].[Member Ordinal] }) ON 1
from [GM_CUST]

Related

Sales amounts of the top n selling vendors by month with other fields in bigquery

i have a table in bigquery like this (260000 rows):
vendor date item_price discount_price
x 2021-07-08 23:41:10 451,5 0
y 2021-06-14 10:22:10 41,7 0
z 2020-01-03 13:41:12 74 4
s 2020-04-12 01:14:58 88 12
....
exactly what I want is to group this data by month and find the sum of the sales of only the top 20 vendors in that month. Expected output:
month vendor_name(top20) sum_of_vendor's_sales sum_of_vendor's_discount item_count(sold)
2020-01 x1 10857 250 150
2020-01 x2 9685 410 50
2020-01 x3 3574 140 45
....
2021 01 x20 700 15 20
2020-02 y1 7421 280 120
2020-02 y2 6500 250 40
2020-02 y3 4500 200 70
.....
2020-02 y20 900 70 30
i tried this (source here). But The desired output could not be obtained.
select month,
(select sum(sum) from t.top_20_vendors) as sum_of_only_top20_vendor_sales
from (
select
format_datetime('%Y%m', date) month,
approx_top_sum(vendor, item_price, 20) top_20_vendors,count(item_price) as count_of_items,sum(discount_price)
from my_table
group by month
) t
Consider below approach
select
format_datetime('%Y%m', date) month,
vendor as vendor_name_top20,
sum(item_price) as sum_of_vendor_sales,
sum(discount_price) as sum_of_vendor_discount,
count(*) as item_count_sold
from your_table
group by vendor, month
qualify row_number() over(partition by month order by sum_of_vendor_sales desc) <= 20

Cumulated Cohorts in SQL

I have the following table :
cohort
month cohort
orders
cumulated orders
2021-01
0
126
126
2021-01
1
5
131
2021-01
2
4
135
2021-02
0
131
131
2021-02
1
9
140
2021-02
2
8
148
And now I want to have the following table where I divide each repeat orders by the number of orders of month 0 :
cohort
month cohort
orders
cumulated orders
cumulated in %
2021-01
0
126
126
100%
2021-01
1
5
131
104%
2021-01
2
4
135
107%
2021-02
0
131
131
100%
2021-02
1
9
140
107%
2021-02
2
8
148
114%
My only hint is to create a CASE statement, but I don't want each month to update the query by adding the line
WHEN cohort="2021-08" THEN cumulated orders / 143
where 143 is the number of orders of cohort 2021-08 at month cohort =0
Has someone got an idea how to get this table ?
A case expression isn't needed. You can use first_value():
select t.*,
( cumulated_order /
first_value(orders) over (partition by cohort order by month_cohort)
) as ratio
from t;
If you really wanted a case, you could use:
select t.*,
( cumulated_order /
max(case when month_cohort = 0 then orders end) over (partition by cohort)
) as ratio
from t;
Consider below
select *,
round(100 * cumulated_orders /
sum(if(month_cohort = 0, orders, 0)) over(partition by cohort)
) as cumulated_in_percent
from `project.dataset.table`
if applied to sample data in your question - output is

How to allocate amount using Oracle SQL in ascending order

I'm not able to build Oracle SQL to Allocate budget amounts to require to projects. I have Budget table and Project tables. I want load into Allocation table as given below.
Budget table:
Category
Budget_Amt
Cat_A
110
Cat_B
20
Cat_C
30
Cat_D
80
Cat_E
100
Project table:
Project
Cost
Proj_1
80
Proj_2
60
Proj_3
50
Proj_4
20
Proj_5
140
Proj_6
30
allocate budget to project in ascending order
Allocation table:
Category
Project
Budget
Cost
Allocation
Cat_A
Proj_1
110
80
80
Cat_A
Proj_2
110
60
30
Cat_B
Proj_2
20
60
20
Cat_C
Proj_2
30
60
10
Cat_C
Proj_3
30
50
20
Cat_D
Proj_3
80
50
30
Cat_D
Proj_4
80
20
20
Cat_D
Proj_5
80
140
30
Cat_E
Proj_5
100
140
100
This is quite a tricky problem. The key idea is to calculate the running budget and running costs -- and then treat this as an overlap between the running amounts.
The following takes this approach:
select p.*, b.*,
least(running_budget_amt + budget_amt, running_cost + cost) - greatest(running_budget_amt, running_cost) as amt
from (select b.*, sum(budget_amt) over (order by category) - budget_amt as running_budget_amt
from budget b
) b join
(select p.*, sum(cost) over (order by project) - cost as running_cost
from project p
) p
on p.running_cost < running_budget_amt + budget_amt and
p.running_cost + cost > running_budget_amt
order by p.project, b.category;
Here is a db<>fiddle.

datagridview 2 table wrong output

Given the following code:
connection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=consignacion.mdb;")
SQLStr = "Select buyer,balance,
(SELECT SUM(salesdb.total)
From salesdb
Where salesdb.buyer = buyerdb.buyer and
buydate=#buydate and
salesdb.total is not Null and
salesdb.total<>#sales ) As [paid]
From buyerdb where balance>#balance"
cmd = New OleDbCommand(SQLStr, connection)
cmd.Parameters.AddWithValue("#buydate", Form1.Lbldate2.Text)
cmd.Parameters.AddWithValue("#balance", 0)
cmd.Parameters.AddWithValue("#sales", 0)
And below the ouput:
name balance sales
BANAL 1000
BAYA 500
RICKY 350
GEN 0
CASH 0
BAGON 0
LOREY 0
TANIS 0 2250
DARWIN 0 345
GLEN 1000 4334
LITO 0 505
LIZA 0 460
CESS 350 984
LOUIE 0 280
YOLLY 0 832
GLENDA 0 170
JOSE 1000 2240
I want to get the followed output:
name balance sales
BANAL 1000
BAYA 500
RICKY 350
TANIS 0 2250
DARWIN 0 345
GLEN 1000 4334
LITO 0 505
LIZA 0 460
CESS 350 984
LOUIE 0 280
YOLLY 0 832
GLENDA 0 170
JOSE 1000 2240
i wanted to display all buyers with balance > 0
i wanted to display all buyer with sales/total >0 at a given date. (sometimes buyers dont have record of sales on a given date.
i don't want to display buyers with balance and sales/total is zero at the same time.
First, use an LEFT JOIN between buyerdb b and salesdb s on the buyer key, and perform a SUM() on these records according a GROUP BY command. It will display buyerdb rows (the left table) even if there are no corresponding salesdb rows:
SELECT b.buyer, b.balance, SUM(s.total) as total
FROM buyerdb b
LEFT JOIN salesdb s on (b.buyer = s.buyer AND s.buydate = #buydate)
GROUP BY buyer
Then, use this request as a nested one, and filtered to get only the rows you want:
SELECT t.buyer, t.balance, t.total
FROM
(
SELECT b.buyer, b.balance, SUM(s.total) as total
FROM buyerdb b
LEFT JOIN salesdb s on (b.buyer = s.buyer AND s.buydate = #buydate)
GROUP BY buyer
) t
-- Filter the rows you don't want (both zero balance and total)
WHERE balance > 0 or total > 0
Working SQL fiddle (should work with Access).

DAX / PP Aggregate a variable project margin down a column

I need a solution similar to this:
DAX running total (or count) across 2 groups
However slightly more complex.
I have the following:
(apologies for the layout - i can't post pictures)
Name Date Monthly Rev Total Rev Margin( % Rev)
Proj 1 1/08/2014 0 7000 15%
Proj 1 1/09/2014 1000 7000 15%
Proj 1 1/10/2014 1000 7000 15%
Proj 1 1/11/2014 1000 7000 15%
Proj 1 1/12/2014 0 7000 15%
Proj 1 1/01/2015 0 7000 15%
Proj 1 1/02/2015 2000 7000 15%
Proj 1 1/03/2015 2000 7000 15%
Proj 2 1/11/2014 0 16000 10%
Proj 2 1/12/2014 1500 16000 10%
Proj 2 2/12/2014 1500 16000 10%
Proj 2 3/12/2014 1500 16000 10%
Proj 2 4/12/2014 1500 16000 10%
Proj 2 5/12/2014 2000 16000 10%
Proj 2 6/12/2014 2000 16000 10%
Proj 2 7/12/2014 0 16000 10%
Proj 2 8/12/2014 2000 16000 10%
Proj 2 9/12/2014 2000 16000 10%
Proj 2 10/12/2014 2000 16000 10%
Monthly rev is the revenue received in a month, total is the total project value and margin is the percentage of revenue. The table is linked to a dates table by Date.
I need to show margin by date (there are other descriptive columns in the table for slicing) however the margin calc is not straightforward.
In an excel table it would look something like this:
Cumm simple margin | Completion| Cumm complex margin | Margin earnt
0 0% 0 0
150 20% 30 30
300 40% 120 90
450 60% 270 150
450 60% 270 0
450 60% 270 0
750 80% 600 330
1050 100% 1050 450
0 0% 0 0
150 11% 17 17
300 22% 67 50
450 33% 150 83
600 44% 267 117
800 56% 444 178
1000 67% 667 222
1000 67% 667 0
1200 78% 933 267
1400 89% 1244 311
1600 100% 1600 356
Where:
Simple margin is calculated on a cumulative basis as % of monthly Rev
Percentage complete of the project is calculated based on "active" months where revenue is earned
Cumulative simple margin is multiplied by the % complete
Actual margin earned in a particular month is the difference between two months.
Note that Monthly revenue is not necessarily continuous.
No idea how to recreate this in power pivot, any suggestions would be well received.
Cheers
Assuming
That your Project 2 data should run monthly from 1/11/2015 to 1/09/2015 (rather than individual December dates)
You have your data in a table called 'ProjectMargins'
Your DateDim table is called 'Reporting Dates'
Then these are the DAX Measures you need (although there may be simpler methods for achieving these results):
[MonthlyRev]:=SUM(ProjectMargins[Monthly Rev])
[ActiveMonth]:=CALCULATE(COUNTROWS('ProjectMargins'),FILTER('ProjectMargins',[MonthlyRev]>0))
[AllActiveMonths]:=CALCULATE([ActiveMonth],ALL('Reporting Dates'[Date]))
[Completion]:=DIVIDE(CALCULATE([ActiveMonth],FILTER(ALL('Reporting Dates'[Date]),'Reporting Dates'[Date] <= MAX(ProjectMargins[Date]))),[AllActiveMonths])
If you need to calculate TotalRev, from your Monthly Rev, Rather than it appearing in the original source table:
[TotalRev]:=IF(ISBLANK(MAX(ProjectMargins[Margin( % Rev)])),BLANK(),CALCULATE([MonthlyRev],ALL('Reporting Dates'[Date])))
[Rev%]:=MAX(ProjectMargins[Margin( % Rev)])
[Cumm Simple Margin]:=CALCULATE([MonthlyRev]*[Rev%],FILTER(ALL('Reporting Dates'[Date]),'Reporting Dates'[Date] <= MAX(ProjectMargins[Date])))
[Cumm Complex Margin]:=[Completion]*[Cumm Simple Margin]
[Previous Month Cumm Complex]:=CALCULATE([Cumm Complex Margin], DATEADD('Reporting Dates'[Date],-1,MONTH))
[Margin Earnt]:=IF([Cumm Complex Margin]>0,[Cumm Complex Margin]-[Previous Month Cumm Complex],BLANK())
NOTE: This assumes that the margin is never negative.
Ensure that the date field from the DateDim table is used in your pivot, not the date field from the Fact table.