get average of subtotal and wet with respect to distinct purchaseorderid and product category in DAX - sql

i don't know where to start, pretty new in power bi or Dax
here is sample data
PurchaseOrderId SubTotal Wet ProductCategory
------------------ ----------- ----------- ------------------
1021 804.9767 233.4432 Wine
------------------ ----------- ----------- ------------------
1022 228.4651 66.2548 Beer
------------------ ----------- ----------- ------------------
1022 228.4651 66.2548 RTD
------------------ ----------- ----------- ------------------
1022 228.4651 66.2548 Wine
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 Wine
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 Beer
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 Non-alcoholic
------------------ ----------- ----------- ------------------
1023 2791.2558 809.4641 RTD
------------------ ----------- ----------- ------------------
1024 396 114.84 Wine
------------------ ----------- ----------- ------------------
1025 374.2325 108.5274 Wine
------------------ ----------- ----------- ------------------
1026 864.093 250.5869 Wine
------------------ ----------- ----------- ------------------
1027 127.9069 37.093 Wine
what i want is i want average order value i.e
(Subtotal+Wet)/Count(Distinct PurchaseOrderId).
For this table Sum of distinct Subtotal is 5586.93 and Sum of
distinct wet is 1620.20,
total number of distinct purchaseorderid i.e number of orders is 7 so my average order value is (5586.93+1620.20)/7 = 1029.59,
i also want average order value by category
i am doing everything in DAX
so how this can be achieved?
Thanks in advance

You need to use this measure: I called my table (Test00) You can use yours in the same way.
AverageOfSubTotal =
VAR TblSummary = ADDCOLUMNS(
SUMMARIZE (Test00, Test00[PurchaseOrderId], Test00[ProductCategory]),
"Total_SubTotal",CALCULATE(SUM(Test00[SubTotal]))
)
RETURN
AVERAGEX(TblSummary,[Total_SubTotal])
If we test it on a table visual:
Now let's test the visual table report performance on DAX Studio:
The results are quite good!! 5 ms query time (4ms FE, 1 ms SE). 4 SE(Storage Engine) queries.

Use this as a new Measure
Avg Subtotal =
AVERAGEX(
SUMMARIZE(
'Table',
'Table'[PurchaseOrderId],
'Table'[ProductCategory],
'Table'[SubTotal]
),
'Table'[SubTotal]
)

Related

SQL statement, result from different row

I have a sales table that look like this
receipt saletype qtysold sellprice discount
31103 ----- I --------- 1 ------- 39 ------- 0
31103 ---- W -------- 1 -------- 0 ------- 14
31103 ----- I --------- 1 ------- 39 ------- 0
31103 ---- W -------- 1 -------- 0 ------- 14
The actual qtysold is 2 pcs from this receipt 31103.
I am trying to get the result from qtysold where salestype='I'
My result will be incorrect when I do this
Select qtysold, sellprice, discount
from salestable
where receipt = '31103'
You can easily return all rows with salestype = 'I' by including this filter in your where clause. To include multiple filters in a single where clause, you would need the 'and' keyword. The code would look something like this-
Select qtysold, sellprice, discount
from salestable
where receipt = '31103'
and salestype = 'I'

Calculate Discount amount Based on Quantity

I have a table
SELECT PRD_CODE,PRD_MAKE,PRD_NAME FROM PRD_MST
SELECT PDI_DISC_QTY,PDI_DISC_PRICE FROM PRD_DISC_INF
select customerid,Productid,quantity from ShoppingCartItem
Records
PRD_MST
PRD_CODE PRD_MAKE PRD_NAME
----------- ---------------- ----------
4 mobile moto e
5 cycle hero
PRD_DISC_INF
PDI_DISC_QTY PDI_DISC_PRICE
----------------- ------------------
2.00 2.10
1.00 2.31
ShoppingCartItem
customerid Productid quantity
----------- ----------- -----------
1 5 5
The Problem is that
if a customer buy 5 mobile and price of single unit is 200 then how do i calculate discount amount base on PDI_DISC_QTY in table 'PRD_DISC_INF'
Need help i am unable to get the logic
Try this DEMO
select PDI_DISC_QTY *
(cast(round(((select quantity from ShoppingCartItem)/PDI_DISC_QTY),0) as int))
from PRD_DISC_INF ;

Creating a view with sums from multiple tables (Oracle SQL)

I'm trying to create a view that gets the sums of a couple of different rows in various tables. (I'm not sure how to explain this properly)
Here is how my tables are set out:
Visitors:
VISITORID FNAME LNAME PHONE HOTELID
---------- --------------- --------------- --------------- ----------
23 Bella Morgan 0394110625 3
Bookings:
BOOKINGID HOTELID ROOMNO BOOKINGDATE BOOKINGDAYS BEDANDBREA VISITORID
---------- ---------- ---------- ------------------- ----------- ---------- ----------
28 3 509 28-04-2013 00:00:00 3 Yes 23
Rooms:
ROOMNO HOTELID ROOMTYPE PRICE
---------- ---------- ------------------------- ----------
509 3 Double 700
Services:
SERVICEID SERVICENAME COST HOTELID
---------- -------------------------------------------------- ---------- ----------
1-CLTH Cloth Cleaning 14.95 1
2-RMSV Room Service 9.95 2
Booking_services:
SERVICEID BOOKINGID
---------- ----------
2-RMSV 32
1-CLTH 32
I want to create a view called bills that gives me the total of room cost and cost of all services.
To get the room price, the sum is rooms.price*bookings.bookingdays.
For the services, it's the sum of all the rows in the services table that match the SERVICEID in booking_services for the matching bookingID.
Currently there are more rows in all of the tables than I've shown (so it doesn't take up too much space on here) and I have a query but it's only showing 2 of the visitors that i'd like the total for. I know it's because of line 5, but I'm not sure how I can get it to calculate that as well as those who do not have a row in booking_services.
Here is that query:
CREATE VIEW bills AS
SELECT v.fname, SUM((r.price*b.bookingdays)+s.cost) AS total
FROM visitors v, rooms r, bookings b, services s, booking_services bs
WHERE v.visitorid = b.visitorid
AND
s.serviceid in(select bs.serviceid from booking_services where bs.bookingid = b.bookingid)
AND
b.roomno = r.roomno
GROUP BY v.fname;
Any help to get what I'm after (if this makes any sense) would be appreciated.
Here is the SQLFiddel Demo
You can try below query for your view:
Select v.fname, sum((r.price*b.bookingdays)+nvl(bso.cost,0))
From visitors v
Join bookings b
on v.visitorid = b.visitorid
Join rooms r
on b.roomno = r.roomno
left outer join (select bs.BOOKINGID,sum(cost) as cost
from booking_services bs
Join services s
on s.SERVICEID = bs.SERVICEID
group by bs.BOOKINGID) bso
on bso.BOOKINGID = b.BOOKINGID
GROUP BY v.fname;

Select column values not present in group by clause

I have a table consisting of customer purchase records having following columns
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 7.99
1 12-FEB-2013 777 O CASH 6.99
1 12-FEB-2013 778 O CARD 9.17
2 23-APR-2013 987 D CASH 7.65
1 15-MAY-2013 1098 T CARD 2.35
I want to aggregate over salenum i,e for each salenum, i want the total price, as well as the store, transtyp, transdate,customer values as these are same for a particular salenum
However if i use
select customer,transdate,salenum,store,transtyp,sum(price) from table1 group by salenum
Its obviously saying not a valid group by value. How to get the desired result?
SAMPLE RESULT:
CUSTOMER TRANSDATE SALENUM STORE TRANSTYP PRICE
-------- --------- --------- ----- --------- ------
1 12-FEB-2013 777 O CASH 15.98
1 12-FEB-2013 778 O CARD 9.17
All non-aggregated columns should be in the group by:
select customer,transdate,salenum,store,transtyp,sum(price)
from table1
group by customer,transdate,salenum,store,transtyp
SELECT CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP,SUM(PRICE)
FROM TABLE1
GROUP BY CUSTOMER,TRANSDATE,SALENUM,STORE,TRANSTYP
ORDER BY CUSTOMER
I using the order by clause because it will be in order and also in sequence.

sum of cost spent on advertisments on specific magazine (month?, year?)

Here are the tables //thanks for fixing my format//
ADV_COST
--------
PAGE_SIZE
MAG_ID
COST
//SAMPLE DATA ADV_COST//
PAGE_SIZE MAG_ID COST
-------------------- ---------- ----------
1/25 PAGE 1 40
1/8 PAGE 1 60
1/6 PAGE 1 65
...
ADS
--------
AD_ID
ADV_ID
PAGE_SIZE
MAG_ID
START_DATE
PURCH_DATE
NUM_ISSUES
//SAMPLE DATA ADS//
AD_ID ADV_ID PAGE_SIZE MAG_ID START_DAT PURCH_DAT NUM_ISSUES
---------- ---------- ---------- ---------- --------- --------- ----------
1 5 1/4 PAGE 1 01-APR-11 01-MAR-11 4
...
Here's the question:
Whirlpool ADV_ID=6; HOUSES: MAG_ID=1;
"How much money did Whirlpool spend in advertising in HOUSES this month?, this year?"
help please, thanks!
This is what I tried.
SQL> SELECT SUM(COST)
2 FROM DVD_ADV_COST A, DVD_ADS B
3 WHERE A.MAG_ID = B.MAG_ID
4 AND B.ADV_ID = 6
5 AND B.MAG_ID = 1;
Try
SELECT SUM(B.COST * A.NUM_ISSUES) AS TOTAL_COST
FROM DVD_ADS A,
DVD_ADV_COST B
WHERE B.MAG_ID = A.MAG_ID
AND B.PAGE_SIZE = A.PAGE_SIZE
AND A.ADV_ID = 6
AND A.MAG_ID = 1
and then add your date range to the WHERE clause as well
If it's not returning any data, make sure that you actually have entries for ADV_ID = 6.... your example show an entry for 5