Hello,
I've got a table like
Zone
Category
Europe
Global
Asia
Global
Ameriaca
Global
Local Grocery
Local
Local Automotive
Local
Local HR
Local
and I want to create a calculated column to look like
Zone
Category
Result
Europe
Global
Europe
Asia
Global
Asia
America
Global
America
Local Grocery
Local
Local
Local Automotive
Local
Local
Local HR
Local
Local
First, you must try yourself how to manage such simple codes...
Result =
IF ( 'Table (2)'[Category] = "Local", "Local", 'Table (2)'[Zone ] )
Related
I have a dataset with multiple user logins and multiple districts as follows:
User
District
Region
UserA
HKG
China
UserA
PRG
Europe
UserA
CKG
China
UserB
LHR
Europe
UserB
AMS
Europe
UserB
TYO
Japan
UserA
CKG
China
UserC
SYD
Australia
UserC
MEL
Australia
UserD
SYD
Australia
UserD
MEL
Australia
I want to see the count of users that have logged into more than a single district within a region
Region
Created Measure
China
1
Europe
1
Japan
0
Australia
2
China has 1, because only UserA has logged into multiple China Districts
Europe has 1, because only UserB has logged into multiple Europe Districts
Japan has 0, because no user has logged into more than 1 Japan District
Australia has 2, because both User C & D have logged onto multiple Australia Districts
I have gotten close with creating a table, but I can only seem to get it to count the distinct values, not the values where the aggregate is greater than 1. This is the DAX I have so far:
DEFINE
TABLE UserGroup =
SUMMARIZECOLUMNS (
User[User],
User[Branch],
User[Region],
"UserCount", COUNT('User'[User]),
"User Distinct Count", DISTINCTCOUNT('User'[User])
)
EVALUATE
GROUPBY(
UserGroup,
UserGroup[Region],
"Failing Formula", SUMX(CURRENTGROUP(), [User Distinct Count])
)
EVALUATE
VAR allValTbl= -- DISTINCT(tbl)
SUMMARIZE(
tbl
,tbl[Region]
,tbl[District]
,tbl[User]
)
VAR moreThenOne=
FILTER(
allValTbl
,CALCULATE(COUNTROWS(tbl))>1
)
RETURN
ADDCOLUMNS(
VALUES(tbl[Region])
,"#DCount",VAR currReg = [Region]
RETURN
COUNTROWS(
FILTER(
moreThenOne
,[Region]=currReg
)
)+0
)
As per the below scenario,I need to exclude all the rows in the table Sales with columns Client,sales,salesdate and region which fall outside the selected maximum and minimum date range of the date slider.
Could anyone please suggest a DAX , I will share my DAX query which is not working.I tried to search official guides in PowerBI for selectedValues() to handle this date filter condition but nothing seems to be working.
I created a Calendar table with distinct values of the dates as present in the Sales Dates column of the Sales table. There is no relationship between these two tables. I used the below DAX measures to handle the filtering criteria for excluding the rows:-
Below measures are meant to find maximum and minimum dates in the selection slider
MaxDate=MAX(DateDimention[Dates])
MinDate=MIN(DateDimention[Dates])
Below ExcludedMeasure is meant to set 1 flag to the values other than selected values which are selected for both min and max dates.
I dragged and dropped it in the visual filter of my table Sales and set it to dont show.But it seems to be not working.
Please suggest how can I fix it.
ExcludeMeasure = IF (SELECTEDVALUE(Calendar,Calendar[MinDate],0,1),
IF (SELECTEDVALUE(Calendar,Calendar[MaxDate],0,1)
My Present Visualization and independent Calendar table to be used in the filter slider:-
Present visualization
Independent Calendar Table
Input data source [Excel]
Client Sales SalesDates Region
A 1000 1.1.2000 USA
A 100 1.2.2000 USA
A 200 4.3.2000 USA
B 110 4.3.2000 Europe
B 1000 5.4.2000 Europe
B 200 6.8.2001 Europe
C 1100 7.9.2001 Asia
C 2000 8.12.2001 Asia
D 100 1.2.2002 Australia
D 1300 6.3.2002 Australia
E 100 7.5.2002 Africa
Expected Results :
If I select Minimum slider date as 01.04.2001 and maximum slider date as 1.09.2001 in my[Dates] filter visualization, then it should return the below results after excluding '5.4.2001' and '6.8.2001':-
Client Sales SalesDates Region
A 1000 1.1.2000 USA
A 100 1.2.2000 USA
A 200 4.3.2000 USA
B 110 4.3.2000 Europe
C 1100 7.9.2001 Asia
C 2000 8.12.2001 Asia
D 100 1.2.2002 Australia
D 1300 6.3.2002 Australia
E 100 7.5.2002 Africa
Kind regards
Sameer
A relationship between the date table and the sales table would be ideal, as when you filter the date from the date table, the filter would exclude the records from the sale tables.
If you can't have a date table with the relationship, your exclude measure needs to be something like this:
Exclude =
CALCULATE(COUNT(Sales[Id]),
Sales[SalesDates] >= [MinDate] &&
Sales[SalesDate] <= [MaxDate]) > 0
Power BI Desktop - I have two tables with Unique value.
Table one contains - Target for Each region.
e.g. APAC - 50, NA - 100, Europe - 200
Table two contains - Sales achievement for each region.
e.g. NA - 70, Europe - 90
Now Problem is - APAC region is not listed in 2nd Table. i created a matrix table and showing both table value in one table through relationship but after making the relationship, APAC region is not showing in third table list because its not there in 2nd table.
So please suggest how can i fix this?
I tried with "Show Items with No data" but it is also not working.
I want the unique list of regions from table 1 & 2 in 3rd table irrespective of value assigned to them or not.
How about creating a third table from unique region-values with DAX?
Something like this:
UniquesTable = DISTINCT(UNION(VALUES(Table1[Region]),VALUES(Table2[Region])))
You should connect Table1 and Table2 to UniquesTable in the Relationships-tab so that calculations with targets and achievements work as expected
I am currently trying to create a loop to deseasonalize several hundred time series which consist of search queries, economic-related words taken from dictionaries. The (working) command my deseasonalization is based on looks like this
sum ACCRUE, meanonly
local mACCRUE =r(mean)
reg ACCRUE January February March April May June July August September October November December, nocons
predict double ACCRUESA, residual
replace ACCRUESA=ACCRUESA+`mACCRUE'
So in the the end I created a new deseasonalized time series called ACCRUESA from the base time series ACCRUE.
In the next step I want to automatize the command for the rest of the queries. I tried the following
foreach var of varlist a-z {
sum `var', meanonly
local mu =r(mean)
reg `var' January February March April May June July August September October November December, nocons
predict double `var'SA, residual
replace `var'SA=`var'SA+`mu'
}
I use a-z to loop through the queries, but maybe this is the wrong approach. My goal was to exclude the monthly dummies. Anyway, after excecuting I get an error that a variable is unknown. You will observe that I tried to create a new variable using the `var' and a suffix, but I am not sure if this approach is feasible.
Does someone have an idea how to improve my command?
The problem seems to be the variable list you give for the loop: a-z. I initially suggested you use _all, instead.
#NickCox correctly pointed out that _all would include undesired variables in the <varlist> (i.e. the months). You can remove those from the <varlist>. Below an example.
clear all
set more off
*------------------- Create example data -----------------------
sysuse auto
foreach var in `=c(Months)' {
gen `var' = 0
}
*------------------ Remove some variables ----------------------
* All variables
ds
local allvars = r(varlist)
display "`allvars'"
* Strings to remove
local removethis = c(Months)
* modified local (no months)
local myvars: list allvars - removethis
display "`myvars'"
*-------------------------- Process ----------------------------
foreach var of varlist `myvars' {
display "`var'"
sum `var', meanonly
display r(mean)
}
This involves using macro lists. Type help macrolists for details.
I have a panel of data (firm-years) that span several countries. For each country I estimate a logit model using the first five years then I use this model to predict probabilities in subsequent years. I foreach loop over the countries and forvalues loop over the subsequent years.
The first few countries work well (both estimations and predictions), but the fifth country's first out-of-sample prediction fails with:
Country: United Kingdom
Year: 1994
too many variables specified
r(103);
The model fits and 1994 has enough data to predict a probability. My predict call is:
predict temp_`c'`y' ///
if (country == "`c'") ///
& (fyear == `y'), ///
pr
Do you have any ideas what could cause this error? I am confused because logit and predict work elsewhere in the same loop. Thanks!
FWIW, here's the .do file.
* generate table 5 from Denis and Osobov (2008 JFE)
preserve
* loop to estimate model by country
levelsof country, local(countries)
foreach c of local countries {
display "Country: `c'"
summarize fyear if (country == "`c'"), meanonly
local est_low = `r(min)'
local est_high = `=`r(min)' + 4'
local pred_low = `=`r(min)' + 5'
local pred_high = `r(max)'
logit payer size v_a_tr e_a_tr re_be_tr ///
if (country == "`c'") ///
& inrange(fyear, `est_low', `est_high')
forvalues y = `pred_low'/`pred_high' {
display "Country: `c'"
display "Year: `y'"
predict temp_`c'`y' ///
if (country == "`c'") ///
& (fyear == `y'), ///
pr
}
}
* combine fitted values and generate delta
egen payer_expected = rowfirst(temp_*)
drop temp_*
generate delta = payer - payer_expected
* table
table country fyear, ///
contents(count payer mean payer mean payer_expected)
*
restore
Update: If I drop (country == "United Kingdom"), then the same problem shifts to the United States (next and last country in panel). If I drop inlist(country, "United Kingdom", "United States") then the problem disappears and the .do file runs through.
You are using country names as part of the new variable name that predict is creating. However, when you get to "United Kingdom" your line
predict temp_`c'`y'
implies something like
predict temp_United Kingdom1812
But Stata sees that as two variable names where only one is allowed.
Otherwise put, you are being bitten by a simple rule: Stata does not allow spaces within variable names.
Clearly the same problem would bite with "United States".
The simplest fudge is to change the values so that spaces become underscores "_". Stata's OK with variable names including underscores. That could be
gen country2 = subinstr(country, " ", "_", .)
followed by a loop over country2.
Note for everyone not up in historical details. 1812 is the year that British troops burnt down the White House. Feel free to substitute "1776" or some other date of choice.
(By the way, credit for a crystal-clear question!)
Here's an another approach to your problem. Initialise your variable to hold predicted values. Then as you loop over the possibilities, replace it chunk by chunk with each set of predictions. That avoids the whole business of generating a bunch of variables with different names which you don't want to hold on to long-term.
* generate table 5 from Denis and Osobov (2008 JFE)
preserve
gen payer_expected = .
* loop to estimate model by country
levelsof country, local(countries)
foreach c of local countries {
display "Country: `c'"
summarize fyear if (country == "`c'"), meanonly
local est_low = `r(min)'
local est_high = `=`r(min)' + 4'
local pred_low = `=`r(min)' + 5'
local pred_high = `r(max)'
logit payer size v_a_tr e_a_tr re_be_tr ///
if (country == "`c'") ///
& inrange(fyear, `est_low', `est_high')
forvalues y = `pred_low'/`pred_high' {
display "Country: `c'"
display "Year: `y'"
predict temp ///
if (country == "`c'") ///
& (fyear == `y'), pr
quietly replace payer_expected = temp if temp < .
drop temp
}
}
generate delta = payer - payer_expected
* table
table country fyear, ///
contents(count payer mean payer mean payer_expected)
*
restore