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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
Syndicate_Admin
Administrator
Administrator

Si el encabezado de columna comienza con el texto "", agregue valores debajo del encabezado de columna para cada fila en M Query

Creé una tabla dinámica en el Editor de consultas usando la columna dinámica. Tengo cuentas financieras como columnas y tengo una etiqueta de proyecto para cada fila. Lo que quiero hacer es crear dos nuevas columnas: una para Ingresos y otra para Costo de Ingresos para cada proyecto. Sé que las cuentas financieras que comienzan con "4" son cuentas de ingresos y las cuentas financieras que comienzan con "5" son cuentas de costos. ¿Hay una instrucción if en M Query que calcule los valores en función de estas condiciones?

4 REPLIES 4
Syndicate_Admin
Administrator
Administrator

Hola @smallfires0628, enfoque similar aquí.

Resultado (se agregaron 2 columnas a la tabla de ejemplo)

dufoq3_0-1709492481376.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRS0lEyNDAAkmbmYCaIsICzQIQRlBWrE63k7AIWAImYmoGETeHqTVE0gdTHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Name" = _t, #"40567-service income" = _t, #"40785 - product income" = _t, #"40037 - additional income" = _t, #"50678 - materials cost" = _t, #"54678 - service cost" = _t, #"56890 - driver fee" = _t, #"56768 - gasoline fee" = _t, #"56780 - additional fee" = _t]),
    // You can delete this step if you have columns with correct types.
    ChangedToNumbers = Table.TransformColumns(Source, {}, each try Number.From(_) otherwise Text.From(_)),
    ColumnNames = List.Buffer(Table.ColumnNames(ChangedToNumbers)),
    StepBack = ChangedToNumbers,
    Ad_Revenue = Table.AddColumn(StepBack, "Revenue", each List.Sum(Record.ToList(Record.SelectFields(_, List.Select(ColumnNames, (x)=> Text.StartsWith(x, "4"))))), type number),
    Ad_CostOfRevenue = Table.AddColumn(Ad_Revenue, "Cost of Revenue", each List.Sum(Record.ToList(Record.SelectFields(_, List.Select(ColumnNames, (x)=> Text.StartsWith(x, "5"))))), type number)
in
    Ad_CostOfRevenue
Syndicate_Admin
Administrator
Administrator

Vea el código a continuación

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnRS0lEyNDAAkmbmYCaIsICzQIQRlBWrE63k7AIWAImYmoGETeHqTVE0gdTHAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Project Name" = _t, #"40567-service income" = _t, #"40785 - product income" = _t, #"40037 - additional income" = _t, #"50678 - materials cost" = _t, #"54678 - service cost" = _t, #"56890 - driver fee" = _t, #"56768 - gasoline fee" = _t, #"56780 - additional fee" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Project Name", type text}, {"40567-service income", Int64.Type}, {"40785 - product income", Int64.Type}, {"40037 - additional income", Int64.Type}, {"50678 - materials cost", Int64.Type}, {"54678 - service cost", Int64.Type}, {"56890 - driver fee", Int64.Type}, {"56768 - gasoline fee", Int64.Type}, {"56780 - additional fee", Int64.Type}}),
    ListOfColumns = List.Buffer(Table.ColumnNames(Source)),
    ColumnCount = Table.ColumnCount(Source) - 1,
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Revenue", each List.Sum(List.Transform({0..ColumnCount}, (x)=> if Text.StartsWith(ListOfColumns{x},"4") then Record.ToList(_){x} else null))),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Cost of Revenue", each List.Sum(List.Transform({0..ColumnCount}, (x)=> if Text.StartsWith(ListOfColumns{x},"5") then List.RemoveLastN(Record.ToList(_),1){x} else null)))
in
    #"Added Custom1"
Syndicate_Admin
Administrator
Administrator

Hola @smallfires0628

Posible solución para la nueva columna a continuación

= si Text.StartsWith([Cuenta], "4") entonces [TuCantidadColumna]
de lo contrario, si Text.StartsWith([Account], "5") then [YourAmountColumn]
else null

Enlace para esta función

Mi tabla se ve más o menos así:

Nombre del proyecto40567-Ingresos por servicios40785 - Ingresos por productos40037 - Ingresos adicionales50678 - Coste de los materiales54678 - Coste del servicio56890 - Tarifa de conductor56768 - Tarifa de gasolina56780 - Tarifa adicionalNueva columna - IngresosNueva columna - Costo de los ingresos
APAGADO10067101810102010
CD20056151815101010

Si he entendido bien, la función text.startswith examinará una columna específica (digamos la columna de ingresos por servicio) y añadirá cantidades en función de si los valores que hay debajo coinciden con una condición. Lo que quiero es algo como esto: si el encabezado de la columna comienza con "4", agregue valores.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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

Top Solution Authors