How to calculate market share in tableau - tableau-api

I have a data set for 10 countries. Each country has more than 8 products and my company has 3 products A,B,C which are sold in each of the country.
So Now, I want to calculate the market share for my products country wise.
for E.g. If total sale for 8 products in country 1 is 100 and sale for products A+B+C is 35, then market share of my company in country1 is 35/100= 35%
.
Please help !

It depends a bit on what you want to do with that afterwards, if you are just interested in the number, do the following:
Create a calculated field TotalSalesPerCountry with {fixed [country]: sum([Sales])} (calculates the total sales per country)
Create a calculated field CompanySalesPerCountry with {fixed [country]: sum(IIF([Product] = 'A' OR [Product] = 'B' OR [Product] = 'C',[Sales],0))} (calculates the total sum per country where [PRODUCT] = A, B or C)
Create a calculated field MarketShare with AVG([CompanySalesPerCountry]) / AVG([TotalSalesPerCountry])
Change the Properties for the last field to 'Percent', drag [Country] to rows and [MarketShare] to columns

Related

Power BI DAX: Group by Monthly Growth Rate

I got the following sample data:
Product Group Product Monthly Start Date Sales Qty
Mobile Phone A Mobile Phone A-1 1/1/2021 100
Mobile Phone A Mobile Phone A-1 2/1/2021 120
Mobile Phone B Mobile Phone B-1 1/1/2021 90
Mobile Phone B Mobile Phone B-1 2/1/2021 78
What I want is to calculate the Monthly product growth rate (below).
Product Group Product Month Start Date Growth Rate
Mobile Phone A Mobile Phone A-1 1/1/2021 null
Mobile Phone A Mobile Phone A-1 2/1/2021 20%
Mobile Phone B Mobile Phone B-1 1/1/2021 null
Mobile Phone B Mobile Phone B-1 2/1/2021 -13%
I guess I need to use groupby and sort order by the Month Start Date and calculate the rate.
Does anyone know the best way of calculating it?
Thanks.
I would do it this way (assuming you are viewing the result with a monthly granularity):
Total Sales Qty = SUM( ExampleTable[Sales Qty] )
MTD Sales Qty =
TOTALMTD( [Total Sales Qty], Dates[Date] )
MTD Sales Qty LM =
CALCULATE( [MTD Sales Qty], DATEADD(Dates[Date], -1, MONTH ) )
MoM Sales Qty Change =
DIVIDE([MTD Sales Qty] - [MTD Sales Qty LM], [MTD Sales Qty LM], BLANK() )
You would calculate month-over-month growth using a sequence of four measures:
sum of the column
amount for the prior month
change month-over-month
change month-over-month %
These DAX patterns can can be used to get you started. Make sure you add the necessary columns to your date table, then modify the measures below with your date table, fact table column to be quantified and measures you create.
DAX Patterns - Month Related Calculations
Sum of the Fact Table Column
Sales Amount:= sum ( SalesTable[Sales Qty] )
Amount for Prior Month
Sales PM :=
VAR CurrentYearMonthNumber = SELECTEDVALUE ( 'Date'[Year Month Number] )
VAR PreviousYearMonthNumber = CurrentYearMonthNumber - 1
VAR Result =
CALCULATE (
[Sales Amount],
REMOVEFILTERS ( 'Date' ),
'Date'[Year Month Number] = PreviousYearMonthNumber
)
RETURN
Result
Change Month-Over-Month
Sales MOM :=
VAR ValueCurrentPeriod = [Sales Amount]
VAR ValuePreviousPeriod = [Sales PM]
VAR Result =
IF (
NOT ISBLANK ( ValueCurrentPeriod ) && NOT ISBLANK ( ValuePreviousPeriod ),
ValueCurrentPeriod - ValuePreviousPeriod
)
RETURN
Result
Change Month-Over-Month %
Sales MOM % :=
DIVIDE (
[Sales MOM],
[Sales PM]
)

Compare two records

My report needs to compare order quotations, grouped by a sales part number. These order quotations generally have mostly the same sales parts within them, but occasionally they differ. I need:
Quote 1 (Q1) - Total sales = Q1 sales qty * Q1 bid price
Quote 2 (Q1) - Total sales = Q2 sales qty * Q2 bid price
Desired info - Total sales = Q2 sales qty * Q1 bid price (If sales part does not exist on Q2, then - Desired info - Total sales = Q1 sales qty * Q1 bid price)
I'm pulling data from a simple SQL query and using the two quote numbers as parameters. How can I accomplish this?

Distinct Count after Sum

So I am looking to do a count after aggregation. Basically I want to be able to total up the Inventory count with a sum and then count how many times each employee has a non zero inventory count.
So for this data Jack/Jimmy would have a count of 1, Sam would have a count of 2 and Steve would have a count of 0. I could easily do this in SQL on the back end but I also want them to be able to use a date parameter. So if they shifted the date to only 1/1/17 Sam would have a count of 1 and everyone else would have a 0. Any help would be much appreciated!
Data
Emp Item Inventory Date
Sam Crackers 1 1/1/2017
Jack Crackers 1 1/1/2017
Jack Crackers -1 2/1/2017
Jimmy Crackers -2 1/1/2017
Sam Apples 1 1/1/2017
Steve Apples -1 1/1/2017
Sam Cheese 1 1/1/2017
With Date>= '1/1/17':
Emp NonZeroCount
Sam 2
Jack 1
Jimmy 1
Steve 0
With Date = '1/1/17':
Emp NonZeroCount
Sam 1
Jack 0
Jimmy 0
Steve 0
SQL I envision it replacing
Create Table #Test(
Empl varchar(50),
Item Varchar (50),
Inventory int,
Date Date
)
Declare #DateParam Date
Set #DateParam = '1/1/17'
Insert into #Test (Empl,Item,Inventory,Date)
Values
('Sam','Crackers',1,'1/1/2017'),
('Jack','Crackers',1,'1/1/2017'),
('Jack','Crackers',-1,'2/1/2017'),
('Jimmy','Crackers',-2,'1/1/2017'),
('Sam','Apples',1,'1/1/2017'),
('Steve','Apples',-1,'1/1/2017'),
('Sam','Cheese',1,'1/1/2017');
Select
Item,Sum(Inventory) as Total
into #badItems
from #Test
Where Date >= #DateParam
group by Item
having Sum(Inventory) <> 0
Select
T.Empl,Count(Distinct BI.Item)
From #Test T
Inner Join #badItems BI on BI.Item = T.Item
group by T.Empl
This is a good case for creating a set in Tableau.
Select the Item field in the data pane on the left, and right click to create a set based on that field. Name it Bad Items, and define it using the following formula on the Condition tab, which assumes you've defined a parameter named [DateParam] of type Date.
sum(if [Date] >= [DateParam] then [Inventory] end) <> 0
You can then use the set on the filter shelf, row shelf, in calculations or combine with other sets as desired.
P.S. I used an alias to display the text "Bad Items" instead of "In" in the table, set a manual default sort order for the Emp field (in case you are trying to reproduce this exactly)

How to achieve matching with same table in postgresql?

I want to count number of sites partition by country on the basis of below criteria:-
For each TrialSite with Trial_Start_Date = X and Site_Activated = Y, you should be counting all rows that meet these conditions:
Trial_Start_Date <= Y, AND
TrialSite_Activation_Date >= X
i.e. all rows where there is some overlapping period with that row's Trial Start Date to TrialSite Activation Date.
sample data example:
Trial id start_date country site_id trialSite_activation_date
Trial A 01-01-2017 India site1 01-02-2017 ----> 2 (only overlaps with itself, and with 2nd row)
Trial A 01-01-2017 India site 2 01-04-2017 ----> 4 (overlaps with all rows, including itself)
Trial B 02-03-2017 India site3 01-04-2017 ----> 3 (does not overlap with first row, since Trial_Start_Date > 01-02-2017)
Trial B 02-03-2017 India site4 01-04-2017 ----> 3
This data can contain multiple countries and this logic needs to be applied with same country records.
You could use the “overlaps” operator && on dateranges:
SELECT t1, count(*)
FROM trial t1
JOIN trial t2
ON daterange(t1.trial_start_date, t1.trialsite_activation_date, '[]')
&& daterange(t2.trial_start_date, t2.trialsite_activation_date, '[]')
GROUP BY t1;
t1 | count
-----------------------------------------------+-------
("Trial A",2017-01-01,India,site1,2017-02-01) | 2
("Trial A",2017-01-01,India,site2,2017-04-01) | 4
("Trial B",2017-03-02,India,site3,2017-04-01) | 3
("Trial B",2017-03-02,India,site4,2017-04-01) | 3
(4 rows)
Instead of using the whole-row reference t1 in the SELECT list, you can specify individual columns there, but then you gave to list them in the GROUP BY clause as well.

Tableau: A bar which shows the difference between

I have the following data for which i need to create a bar chart as shown in the picture in tableau:
Brand Sales week
A 12 1
B 20 1
C 14 1
A 12 2
B 22 2
C 16 2
A 18 3
B 16 3
C 27 3
My chart must contain sales in rows and week in columns. For every week, I must show three bars:
Sales for Brand A (A is always fixed)
sales for Brand B/ Brand C (B/C:- Its a parameter selection)
Difference between sales of these two
Is this what you asking for ??
You can do this by creating calculated fields of the sales for Brand A, Brand B/C (based on selection), and the difference between those two calculated fields:
Brand A Sales: SUM(IF [Brand] = 'A' THEN [Sales] END)
Brand B/C Sales: SUM(IF [Brand] != 'A' THEN [Sales] END)
Brand Difference: [Brand A Sales] - [Brand B/C Sales]
The calculation is dependent on your filter being setup properly, so make sure you have something like this calculated field in your filter card and set to True:
Brand Filter: [Brand Select] = [Brand] OR [Brand] = 'A' [Brand Select] is the parameter to select between B or C
Lastly, place [Measure Names] in the columns section and [Measure Values] in the rows section. Be sure only to include the 3 calculated fields mentioned above.
You should get a result like so:
Brand B -
Brand C -
Note: The calculation for the difference between brands may need to be altered based on your expected output. I based mine in the example off of what I assumed you had within the image in your post.