Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Cálculo del mejor trimestre/año

Me gustaría construir una tabla calculada que devuelva el mejor trimestre/año en términos de cantidad de ventas.

Esta es la tabla Datos

QtrYearQtrNumberSalesQty
Qtr1-2018180
Qtr1-2019189
Qtr1-2020173
Qtr2-2018285
Qtr2-2019288
Qtr2-2020279
Qtr3-2018386
Qtr3-2019384
Qtr3-2020383
Qtr4-2018484

Creo que esto no es difícil, simplemente no pude averiguar cómo hacerlo

Esta es la tabla de salida deseada

BestQtrYearSalesQty
Qtr1-201989
Qtr2-201988
Qtr3-201886
Qtr4-201985

He intentado groupby, pero sólo devuelve el número qtr, no el año trimestre

QtrNumberSalesQty
189
288
386
485
1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

@MikeKK, de hecho, se puede lograr mediante Power Query y DAX.

Solución Power Query,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc5BDoAgDETRu3SNSSlV6DFcEy5hvH8U4iBddPMy+WmtdN5X3IRjoUDxvcLUws8GtpWFP84JLIhIX++ODVxWHpHOebYTIqmvD8cG1pVHZPD8RBFRt1ZE1D2oiGDdHg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QtrYear = _t, QtrNumber = _t, SalesQty = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"QtrYear", type text}, {"QtrNumber", Int64.Type}, {"SalesQty", Int64.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"QtrNumber"}, {{"Best", each [best=List.Max([SalesQty]), result=Table.SelectRows(_, each [SalesQty]=best)][result]}}),
    #"Expanded Best" = Table.ExpandTableColumn(#"Grouped Rows", "Best", {"QtrYear", "SalesQty"}, {"QtrYear", "SalesQty"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Best",{"QtrNumber"})
in
    #"Removed Columns"

Screenshot 2020-11-15 163218.png

Solución de tabla calculada DAX

Best Qtr = 
VAR __t = SUMMARIZECOLUMNS ( Sales[QtrNumber], "Best", MAX ( Sales[SalesQty] ) )
VAR __best =
    ADDCOLUMNS (
        __t,
        "QtrYear", CALCULATE ( MAX ( Sales[QtrYear] ), Sales[SalesQty] = EARLIER ( [Best] ) )
    )
RETURN
    __best

Screenshot 2020-11-15 163352.png

Solución de medida DAX

Best Sales = 
VAR __best =
    MAXX (
        DISTINCT ( 'Sales'[QtrNumber] ),
        CALCULATE ( MAX ( Sales[SalesQty] ), ALLEXCEPT ( Sales, Sales[QtrNumber] ) )
    )
RETURN
    IF ( MAX ( Sales[SalesQty] ) = __best, __best )

Screenshot 2020-11-15 171543.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

2 REPLIES 2
CNENFRNL
Community Champion
Community Champion

@MikeKK, de hecho, se puede lograr mediante Power Query y DAX.

Solución Power Query,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc5BDoAgDETRu3SNSSlV6DFcEy5hvH8U4iBddPMy+WmtdN5X3IRjoUDxvcLUws8GtpWFP84JLIhIX++ODVxWHpHOebYTIqmvD8cG1pVHZPD8RBFRt1ZE1D2oiGDdHg==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [QtrYear = _t, QtrNumber = _t, SalesQty = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"QtrYear", type text}, {"QtrNumber", Int64.Type}, {"SalesQty", Int64.Type}}),

    #"Grouped Rows" = Table.Group(#"Changed Type", {"QtrNumber"}, {{"Best", each [best=List.Max([SalesQty]), result=Table.SelectRows(_, each [SalesQty]=best)][result]}}),
    #"Expanded Best" = Table.ExpandTableColumn(#"Grouped Rows", "Best", {"QtrYear", "SalesQty"}, {"QtrYear", "SalesQty"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded Best",{"QtrNumber"})
in
    #"Removed Columns"

Screenshot 2020-11-15 163218.png

Solución de tabla calculada DAX

Best Qtr = 
VAR __t = SUMMARIZECOLUMNS ( Sales[QtrNumber], "Best", MAX ( Sales[SalesQty] ) )
VAR __best =
    ADDCOLUMNS (
        __t,
        "QtrYear", CALCULATE ( MAX ( Sales[QtrYear] ), Sales[SalesQty] = EARLIER ( [Best] ) )
    )
RETURN
    __best

Screenshot 2020-11-15 163352.png

Solución de medida DAX

Best Sales = 
VAR __best =
    MAXX (
        DISTINCT ( 'Sales'[QtrNumber] ),
        CALCULATE ( MAX ( Sales[SalesQty] ), ALLEXCEPT ( Sales, Sales[QtrNumber] ) )
    )
RETURN
    IF ( MAX ( Sales[SalesQty] ) = __best, __best )

Screenshot 2020-11-15 171543.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

ryan_mayu
Super User
Super User

@jjkk

tal vez usted puede crear una nueva mesa para esto.

Table 2 = 
VAR tbl=SUMMARIZE('Table','Table'[QtrNumber],"qty",max('Table'[SalesQty]))
return ADDCOLUMNS(tbl,"yearqty",maxx(FILTER('Table','Table'[QtrNumber]=EARLIER('Table'[QtrNumber])&&[qty]='Table'[SalesQty]),'Table'[QtrYear]))

1.PNG2.PNG





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.