TOPN in PowerBI DAX not arranging values in proper order - powerbi

I have been running into some issues with the TOPN function in DAX in PowerBI.
Below is the original dataset:
regions sales
--------------
a 1191
b 807
c 1774
d 376
e 899
f 1812
g 1648
h 6
i 1006
j 1780
k 243
l 777
m 747
n 61
o 1637
p 170
q 1319
r 1437
s 493
t 1181
u 118
v 1787
w 1396
x 102
y 104
z 656
So now, I want to get the Top 5 sales in descending order.
I used the following code:
Table = TOPN(5, SUMMARIZE(Sheet1, Sheet1[regions], Sheet1[sales]), Sheet1[sales], DESC)
The resulting table is as follows:
regions sales
--------------
g 1648
j 1780
c 1774
v 1787
f 1812
Any idea why this is happening?

According to Microsoft documentation this is working as intended.
https://msdn.microsoft.com/en-us/query-bi/dax/topn-function-dax
Remarks
TOPN does not guarantee any sort order for the results.
What you can do is to create a RANKX to sort by.

Related

Using factor inputs in a caret-driven shiny app

I am trying to develop a simple shiny app which takes in patient data and predicts the probability of a disease condition using caret.
Diagnosis
Age
Creatine
Chronic
70
765
Chronic
80
784
Chronic
72
692
Chronic
88
965
Chronic
68
1065
Chronic
75
1005
Acute
56
445
Acute
67
378
Acute
78
501
Acute
45
678
Acute
37
776
Acute
39
644
The following code works and returns a probability value.
library(shiny)
library(caret)
library(readxl)
hepC_lite <- read_excel("hepC_lite.xlsx")
model_mars <- train(Diagnosis ~ ., data = hepC_lite, method = "earth")
ui <- fluidPage(
numericInput("age", label = "Age", value = 50, min = 1, max = 99),
numericInput("creatine", label = "Creatine", value = 100, min = 1, max = 2000),
actionButton("submitButton", "Submit"),
tableOutput("userDefinedTable"),
textOutput('probability')
)
server <- function(input, output) {
values <- reactiveValues()
observeEvent(input$submitButton, {
values$new_row <- data.frame(Age = input$age, Creatine = input$creatine)
values$predicted_mars <- predict(model_mars, values$new_row, type="prob")[,2]
})
output$userDefinedTable <- renderTable(values$new_row)
output$probability <- renderText(values$predicted_mars)
}
shinyApp(ui = ui, server = server)
To include a factor variable (Gender) in the prediction, I am first one-hot encoding the dataset.
Diagnosis
Age
Creatine
Gender
Chronic
70
765
M
Chronic
80
784
M
Chronic
72
692
F
Chronic
88
965
M
Chronic
68
1065
M
Chronic
75
1005
F
Acute
56
445
F
Acute
67
378
F
Acute
78
501
F
Acute
45
678
M
Acute
37
776
M
Acute
39
644
F
#one-hot encoding
x = hepC_lite[, 2:4]
y = hepC_lite$Diagnosis
dummy_model <- dummyVars(Diagnosis ~ ., data = hepC_lite)
trainData <- predict(dummy_model, newdata = hepC_lite)
hepC_lite <- data.frame(trainData)
hepC_lite$Diagnosis <- y
How should I edit the following line to include the Gender variable?
values$new_row <- data.frame(Age = input$age, Creatine = input$creatine)
Running this line with Gender = input$gender causes an error - Warning: Error in eval: object 'Gender.F' not found

Match values within 3 different tables in PowerBI

I have 3 tables in PowerBI
Table 1
Num
111
222
333
Table 2
Number Code
111 aa
333 cc
222 bb
444 ff
666 gg
These 2 tables are connected by the Number column
Which means the connected value looks like this-
Number Code
111 aa
222 bb
333 cc
Now on my table 3 I have the following -
Table 3
Number Code
111 aa
222 bc
222 bb
444 ff
666 gg
Now what I would like to do is to compare the code when the Number Matches. Means the Output should look like -
Number Code Result
111 aa Y
222 bc N
222 bb N
444 ff N
666 gg N
Do anyone knows any solution to solve this challenge!
I'm not sure how Table 1 is relevant but it seems like you can just do a lookup and check if it matches.
Result =
IF (
LOOKUPVALUE (
Table2[Code],
Table2[Number], Table3[Number]
) = Table3[Code],
"Y",
"N"
)

check each variable of a column pandas

I have a pivot table:
Type Result
l 213
l 435
h 54
l 34
h 54
l 645
h 345
h 34
h 345
l 345
l 666
I want to calculate the result column depending on it's type colume 'l' or 'h':
for h, f(result);
for l, g(result);
Finally, append the calculation results as a new column in this pivot table.
Can anyone help me with this?

Why Sum of Squares were different between Stata anova and SAS glm?

anova y group hop
Number of obs = 206 R-squared = 0.0331
Root MSE = 20.0345 Adj R-squared = 0.0139
Source | Partial SS df MS F Prob > F
-----------+----------------------------------------------------
Model | 2761.85468 4 690.463671 1.72 0.1469
|
group | 42.2798948 1 42.2798948 0.11 0.7459
hop | 2633.73186 3 877.910619 2.19 0.0907
|
Residual | 80677.5664 201 401.380927
-----------+----------------------------------------------------
Total | 83439.4211 205 407.021566
proc glm data=ccc;
class group hop;
model y=group hop;
run;
Sum of
Source DF Squares Mean Square F Value Pr > F
Model 4 2761.79407 690.44852 1.72 0.1469
Error 201 80677.50607 401.38063
Corrected Total 205 83439.30014
R-Square Coeff Var Root MSE hbalcv27 Mean
0.033099 129.8628 20.03449 15.42743
Source DF Type I SS Mean Square F Value Pr > F
group 1 128.138891 128.138891 0.32 0.5727
HOP 3 2633.655176 877.885059 2.19 0.0907
Source DF Type III SS Mean Square F Value Pr > F
group 1 42.289824 42.289824 0.11 0.7458
HOP 3 2633.655176 877.885059 2.19 0.0907
Perhaps the storage precision is not the same in the SAS and Stata data sets. The computations could also be done in different precisions. I don't know about SAS, but according to this blog by Bill Gould:
Stata does all calculations in double (and sometimes quad) precision.

In the following SAS statement, what do the parameters "noobs" and "label" stand for?

In the following SAS statement, what do the parameters "noobs" and "label" stand for?
proc print data-sasuser.schedule noobs label;
per SAS 9.2 documentation on PROC PRINT:
"NOOBS - Suppress the column in the output that identifies each observation by number"
"LABEL - Use variables' labels as column headings"
noobs don't show you the column of observations number
(1,2,3,4,5,....)
my first title
results without noobs
Obs name sex group height weight
1 mike m a 21 150
2 henry m b 30 140
3 norian f b 18 130
4 nadine f b 32 135
5 dianne f a 23 135
results with noobs
my first title
name sex group height weight
mike m a 21 150
henry m b 30 140
norian f b 18 130
nadine f b 32 135
dianne f a 23 135