Date Table created with M is imported as an Expression - ssas

I am using SSAS Tabular 2019 and I have had no problem in loading my views or tables, but this time instead of loading a Date table from SQL Server I created my own Date table using custom M code, but it appears that the date table is being imported as an Expression and not as a table. Here is a screenshot:
Here is my M code:
let
Source = ( StartDate as date, EndDate as date ) as table =>
let
DateTable =
let
StartDateNumber = Number.From ( Date.StartOfYear ( StartDate ) ),
EndDateNumber = Number.From ( Date.AddYears ( Date.EndOfYear ( EndDate ), 1 ) ),
DateList = { StartDateNumber..EndDateNumber },
ToTable = Table.FromList ( DateList, Splitter.SplitByNothing() ),
ToDateType = Table.TransformColumnTypes ( ToTable, { "Column1", type date } ),
RenameToDate = Table.RenameColumns ( ToDateType, { "Column1", "Date" } )
in
RenameToDate,
// Day related fields are created here
Day_Fields =
let
Add_Day_Name = Table.AddColumn ( DateTable, "Weekday Name", each Date.DayOfWeekName ( [Date] ), type text ),
Add_Day_Number = Table.AddColumn ( Add_Day_Name, "Weekday Number", each Date.DayOfWeek ( [Date] ), Int64.Type )
in
Add_Day_Number,
// Month related fields are created here
Month_Fields =
let
Add_Month_Name = Table.AddColumn ( Day_Fields, "Month Name", each Date.MonthName ( [Date] ), type text ),
Add_Month_Number = Table.AddColumn ( Add_Month_Name, "Month Number", each Date.Month ( [Date] ), Int64.Type ),
Add_Month_Initials = Table.AddColumn ( Add_Month_Number, "Month Initials", each Text.Start ( [Month Name], 3 ), type text )
in
Add_Month_Initials,
// Year Related Fields are Create Here
Year_Fields =
let
Add_Year = Table.AddColumn ( Month_Fields, "Calendar Year Number", each Date.Year ( [Date] ), Int64.Type ),
Add_Year_Text = Table.AddColumn ( Add_Year, "Calendar Year", each "CY " & Text.From ( Date.Year ( [Date] ) ), type text )
in
Add_Year_Text
in
Year_Fields
in
Source
Can you please confirm what is happening here?

Related

Simple Having Clause created with Power BI measure

I created this simple table in SQL:
create table testTable (
date date not null,
leader varchar(20),
name varchar(20)
)
insert into testTable
values
('2021-01-01', 'KIM', 'Anders'),
('2021-01-02', 'KIM', 'Annika'),
('2021-01-03', 'KIM', 'Anna'),
('2021-01-04', 'KIM', 'Anna'),
('2021-01-03', 'KIM', 'Annika'),
('2021-01-01', 'JOHAN', 'Sara'),
('2021-01-02', 'JOHAN', 'Sara'),
('2021-01-03', 'JOHAN', 'Sara')
I am trying to get an ekvivalent solution to the following code in a dax measure if possible
select max(leader), name, count(name)
from testTable
group by name
having count(name) >= 2
The result that im looking for is.
Leader
Measure
KIM
2
JOHAN
1
Think about HAVING as a filter that happens after a grouping. So something like
Measure = COUNTROWS(filter(SUMMARIZECOLUMNS('Table'[Name],"Count",count('Table'[Name])), [Count]>=2))
And here's a simple way to present test data for DAX questions, entirely in DAX:
testTable = SELECTCOLUMNS
(
{
(date(2021,01,01),"KIM","Anders")
,(date(2021,01,02),"KIM","Annika")
,(date(2021,01,03),"KIM","Anna")
,(date(2021,01,04),"KIM","Anna")
,(date(2021,01,03),"KIM","Annika")
,(date(2021,01,01),"JOHAN","Sara")
,(date(2021,01,02),"JOHAN","Sara")
,(date(2021,01,03),"JOHAN","Sara")
}, "date", [Value1]
, "leader", [Value2]
, "name", [Value3]
)
This is much easier way to reproduce a scenario than creating a table in SQL Server, and loading it through Power Query, or using the "Enter Data" form in PowerBI which creates the table in Power Query.
Edit: after adding the desired result to the question, the answer changes like follows
A possible solution is to implement a measure that counts the number of names that appear more than once for the selected leader
# Names ge 2 =
COUNTROWS (
FILTER (
VALUES ( Test[name] ),
CALCULATE ( COUNTROWS ( Test ), ALLEXCEPT ( Test, Test[name], Test[leader] ) ) > 1
)
)
here is a working example on dax.do
DEFINE
TABLE Test =
DATATABLE (
"date", DATETIME,
"leader", STRING,
"name", STRING,
{
{ "2021-01-01", "KIM", "Anders" },
{ "2021-01-02", "KIM", "Annika" },
{ "2021-01-03", "KIM", "Anna" },
{ "2021-01-04", "KIM", "Anna" },
{ "2021-01-03", "KIM", "Annika" },
{ "2021-01-01", "JOHAN", "Sara" },
{ "2021-01-02", "JOHAN", "Sara" },
{ "2021-01-03", "JOHAN", "Sara" }
}
)
MEASURE Test[# Names ge 2] =
COUNTROWS (
FILTER (
VALUES ( Test[name] ),
CALCULATE ( COUNTROWS ( Test ), ALLEXCEPT ( Test, Test[name], Test[leader] ) ) > 1
)
)
EVALUATE
SUMMARIZECOLUMNS (
Test[leader],
"# Names ge 2", [# Names ge 2]
)
and the resulting output
I've left the measure of my previous answer on the original dax.do, that returned this output

How can convert SQL to lambda or LINQ

How can I convert below SQL to lambda or LINQ?
with cte
as (select * from test1
union all
select * from test2)
select * from cte
union all
select sum(columnA),sum(columnB),sum(columnC) from cte
In Linq UNION ALL is .Concat(), so:
var cte = test1.Concat(test2);
var sums = new MyModel
{
columnA = cte.Sum(c => c.columnA),
columnB = cte.Sum(c => c.columnB),
columnC = cte.Sum(c => c.columnC),
}
return cte.Concat(IEnumerable.Repeat(sums, 1));
You must remember that test1 and test2 must be type MyModel and MyModel contains only columnA, columnB and columnC.
I put two tables together in one datagridvie but in the last row of datagridview I need the total for both tables in the country, I can do one row in total for one table and another row for the other table I also don't need it, like I can only have one line with the total of both tables.
DataContex db = new DataContex();
var query = (
from v1 in db.View1
where shf.Date >= dpDate.Value && shf.Date <= dpDate1.Value
select new
{
v1.Name,
v1.Date,
v1.Quality,
v1.Rat,
v1.Total
}
).Concat
(
from v2 in db.View2
where f.Date >= dpDate.Value && f.Date <= dpDate1.Value
select new
{
v2.Name,
v2.Date,
v2.Quality,
v2.Rat,
v2.Total
}
).Concat
(from View2 in
(from v2 in db.View2
where v2.Date >= dpDate.Value && sh.Date <= dpDate1.Value
select new
{
v2.Name,
v2.Date,
v2.Quality,
v2.Rate,
v2.Total
})
group v2 by new { v2.NRFA } into g
select new
{
Name = "Total:",
Date = dpDate1.Value,
Quality = (decimal?)g.Sum(p => p.Quality),
Rate = (decimal?)g.Sum(p => p.Rate),
Total = (decimal?)g.Sum(p => p.Total)
}
);
Blockquote

DAX expression for ROW_NUMBER() PARTITION BY ORDER BY equivalent

I have a SQL statement like this:
(ROW_NUMBER() OVER (PARTITION BY a.[market], [MEASURE_TYPE]
ORDER BY AM, REP, ORDER_KEY)) AS ORDER_KEY
I want to write a DAX to implement the above SQL statement.
This is not as simple in DAX as in SQL. Here is an example:
Order Key Within Partition =
VAR CurrentMarket = [Market]
VAR CurrentMeasureType = [MeasureType]
VAR CurrentAM = [AM]
VAR CurrentREP = [REP]
VAR CurrentOrderKey = [OrderKey]
VAR CurrentPartition = FILTER (
a, -- the table name
[Market] = CurrentMarket
&& [MeasureType] = CurrentMeasureType
)
RETURN SUMX (
CurrentPartition,
IF (
ISONORAFTER (
CurrentAM, [AM], ASC,
CurrentREP, [REP], ASC,
CurrentOrderKey, [OrderKey], ASC
),
1
)
)
EDIT: Power Query would be better to achieve this.
let
/* Steps so far */
Source = ...,
...
a = ...,
/* End of steps so far */
/* Add steps below to add Order Key Within Partition column */
Partitions = Table.Group(
a,
{"Market", "MeasureType"}, {"Partition", each _}
)[Partition],
AddedOrderKeys = List.Transform(
Partitions,
each Table.AddIndexColumn(
Table.Sort(_, {"AM", "REP", "OrderKey"}),
"Order Key Within Partition",
1
)
),
Result = Table.Combine(AddedOrderKeys)
in
Result
I contribute with an alternative solution to the RANKX. The answer containing the Power Query is the correct one because avoid using calculated columns.
Sales[Sequence by Customer] =
VAR CurrentDate = Sales[Date]
VAR CurrentTime = Sales[Time]
RETURN COUNTROWS (
    FILTER (
        CALCULATETABLE (
            Sales,
            ALLEXCEPT ( Sales, Sales[Customer] )
        ),
        Sales[Date] < CurrentDate
          || ( Sales[Date] = CurrentDate
               && Sales[Time] <= CurrentTime )
    )
)
Source

Some Strange Fault (In My Model Record ?)

My compiler complains about two values:
model.firstChord.fifth and model.secondChord.fifth
in this excerpt:
-- render frets
renderFret : Model -> Fret -> Html Msg
renderFret model fret =
let
( pitchName, pitchLevel ) = fret.pitch
( firstChordRootPitchName, firstChordRootPitchLevel ) = model.firstChord.root
( firstChordThirdPitchName, firstChordThirdPitchLevel ) = model.firstChord.third
( firstChordFifthPitchName, firstChordFifthPitchLevel ) = model.firstChord.fifth
( secondChordRootPitchName, secondChordRootPitchLevel ) = model.secondChord.root
( secondChordThirdPitchName, secondChordThirdPitchLevel ) = model.secondChord.third
( secondChordFifthPitchName, secondChordFifthPitchLevel ) = model.secondChord.fifth
in
...
it tells me:
model.firstChord does not have a field named fifth. - The type of model.firstChord is:
Maybe Chord
Which does not contain a field named fifth.
but my model has a field fifth:
-- initial model
init : ( Model, Cmd Msg )
init =
(
{ firstChord =
Just
{ root = ( "C", 3 )
, third = ( "E", 3 )
, fifth = ( "G", 3 )
}
, secondChord =
Just
{ root = ( "F", 3 )
, third = ( "A", 3 )
, fifth = ( "C", 4 )
}
}
,
Cmd.none
)
The type of the chord is:
-- chords
type alias Chord =
{ root : Pitch
, third : Pitch
, fifth : Pitch
}
Each pitch has this type:
-- pitch
type alias Pitch = ( PitchName, PitchLevel )
-- pitch name
type alias PitchName = String
-- pitch level
type alias PitchLevel = Int
Where could be the problem?
Thank you.
Compile error says exactly what the problem is
Maybe Chord is either Just Chord or Nothing. None of these two contain a field named fifth.
In order to make this work you need to make sure that model.firstChord and model.secondChord are Just Chord:
-- render frets
renderFret : Model -> Fret -> Html Msg
renderFret model fret =
case (model.firstChord, model.secondChord) of
(Just firstChord, Just secondChord) ->
let
( pitchName, pitchLevel ) = fret.pitch
( firstChordRootPitchName, firstChordRootPitchLevel ) = firstChord.root
( firstChordThirdPitchName, firstChordThirdPitchLevel ) = firstChord.third
( firstChordFifthPitchName, firstChordFifthPitchLevel ) = firstChord.fifth
( secondChordRootPitchName, secondChordRootPitchLevel ) = secondChord.root
( secondChordThirdPitchName, secondChordThirdPitchLevel ) = secondChord.third
( secondChordFifthPitchName, secondChordFifthPitchLevel ) = model.secondChord.fifth
in
...
_ ->
-- here is something when either `model.firstChord` or `model.secondChord` is `Nothing`
By using pattern matching (Just firstChord, Just secondChord), firstChord and secondChord expressions appear to be of type Chord, which has a field named fifth
You have not provied your Model, but in your init function you have declared the chord as a Maybe. If the compiler was happy with that, then it means your model also includes the Maybes. As a first solution, remove the Justs, but also look at your Model
init =
(
{ firstChord =
Just
{ root = ( "C", 3 )
, third = ( "E", 3 )
, fifth = ( "G", 3 )
}
, secondChord =
Just <-------- Here you say that secondChord is Maybe Chord
{ root = ( "F", 3 )
, third = ( "A", 3 )
, fifth = ( "C", 4 )
}
}
,
Cmd.none
)

MDX - Extract Last 24 hours data

I need to extract last 24 hours data from a cube. Below is the MDX I have written but it doesn't return the data according to current time (system time).
SELECT
[Date].[Calender-Year_Quarter_Month_Date].[Date] ON ROWS
,{[Measures].[Delay In Mintues]} ON COLUMNS
FROM
(
SELECT
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now() - 1
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&["+FORMAT(NOW(),"HH")+"]
:
[Time].[Time-Hour_Time].[Hour].&[23].&[23:59]
)
}
+
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now()
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&[0].&[00:00]
:
[Time].[Time-Hour_Time].[Hour].&["+FORMAT(NOW(),"HH")+"]
)
} ON COLUMNS
FROM [Delay Reasons]
);
(note: not tested)
Does this work?
SELECT
[Date].[Calender-Year_Quarter_Month_Date].[Date] ON ROWS
,{[Measures].[Delay In Mintues]} ON COLUMNS
FROM [Delay Reasons]
WHERE
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now() - 1
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&["+FORMAT(NOW(),"HH")+"]
:
[Time].[Time-Hour_Time].[Hour].&[23].&[23:59]
)
}
+
{
StrToMember
("[Date].[Calender-Year_Quarter_Month_Date].[Date].&["
+
Format
(
Now()
,'yyyyMMdd'
)
+ "]"
)
*
(
[Time].[Time-Hour_Time].[Hour].&[0].&[00:00]
:
StrToMember
(
"[Time].[Time-Hour_Time].[Hour].&[" + Format(Now(),"HH") + "].&[00:00]"
)
)
};