The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
Estoy trabajando en un proyecto de Power BI y he encontrado un desafío que no he podido resolver. Necesito ayuda para entender cómo distribuir correctamente las "room nights" (RN) de las reservas en función de las fechas de entrada y salida.
Quiero crear una medida en DAX que distribuya las "room nights" de cada reserva entre los días que abarca la estancia, de manera que:
Código Actual:
Aquí está la medida DAX que he estado utilizando, pero solo consigo darle sentido a los datos si ubico el total de RN de cada reserva a la fecha de entrada (el filtro son condiciones extra que queremos hacer en el análisis).
¿Podrían ayudarme a ajustar esta medida para que reparta las "room nights" correctamente entre las fechas de entrada y salida?
Hi @Arnauar97
¡Claro! Aquí tienes la explicación y el código ajustado en español:
RN_Distribuidas =
VAR FechaSeleccionada = MAX ( A0Dim_Fecha[Date] ) // La fecha seleccionada en el reporte
VAR TablaFiltrada =
FILTER (
Fact_ReservasPickup,
Fact_ReservasPickup[FECHAENTRADA] <= FechaSeleccionada && // Reservas con fecha de entrada en o antes de la fecha seleccionada
Fact_ReservasPickup[FECHASALIDA] >= FechaSeleccionada && // Reservas con fecha de salida en o después de la fecha seleccionada
( ISBLANK(Fact_ReservasPickup[FECHA_CANCELACION]) || Fact_ReservasPickup[FECHA_CANCELACION] >= FechaSeleccionada ) // No canceladas antes de la fecha seleccionada
)
VAR NochesPorDia =
ADDCOLUMNS (
TablaFiltrada,
"DiasDeEstancia", DATEDIFF ( Fact_ReservasPickup[FECHAENTRADA], Fact_ReservasPickup[FECHASALIDA], DAY ) + 1, // Calcular los días de estancia, inclusive
"NochesPorDia", DIVIDE ( Fact_ReservasPickup[RN OTB], DATEDIFF ( Fact_ReservasPickup[FECHAENTRADA], Fact_ReservasPickup[FECHASALIDA], DAY ) + 1 ) // Distribuir RN entre los días
)
RETURN
SUMX (
NochesPorDia,
[NochesPorDia] // Sumar las noches de habitación por día para la fecha seleccionada
)
Si una reserva abarca desde el 28 de marzo hasta el 4 de abril con 6 noches de habitación (RN OTB = 6), entonces cada día entre el 29 de marzo y el 4 de abril recibirá 1 noche de habitación.
Puedes probar esta medida en una visualización de tabla con la fecha seleccionada en las filas para ver cómo se distribuyen las noches de habitación a través de los días. Para que funcione correctamente, es recomendable que exista una relación entre tu tabla de fechas (A0Dim_Fecha) y tu tabla de reservas (Fact_ReservasPickup).
Esta medida ahora debería distribuir las noches de habitación correctamente entre cada día del período de estancia para cada reserva. ¡Déjame saber si necesitas más ajustes!
ENGLISH VERSION FOR THE ABOVE ANSWER :
Sure! I understand your goal now: you want to distribute the total "room nights" (RN OTB) across each day between the CHECK-IN DATE and DEPARTURE DATE for every reservation. The current DAX measure you provided is summing the RN OTB based on the selected date, but it doesn’t account for distributing the room nights over the full stay period.
Here’s how you can adjust your DAX measure to distribute the "room nights" correctly:
RN_Distributed =
VAR SelectedDate = MAX ( A0Dim_Fecha[Date] ) // The selected date in the report
VAR FilteredTable =
FILTER (
Fact_ReservasPickup,
Fact_ReservasPickup[FECHAENTRADA] <= SelectedDate && // Reservations with check-in on or before the selected date
Fact_ReservasPickup[FECHASALIDA] >= SelectedDate && // Reservations with check-out on or after the selected date
( ISBLANK(Fact_ReservasPickup[FECHA_CANCELACION]) || Fact_ReservasPickup[FECHA_CANCELACION] >= SelectedDate ) // Not canceled before the selected date
)
VAR RoomNightsPerDay =
ADDCOLUMNS (
FilteredTable,
"DaysInStay", DATEDIFF ( Fact_ReservasPickup[FECHAENTRADA], Fact_ReservasPickup[FECHASALIDA], DAY ) + 1, // Calculate days of stay, inclusive
"RoomNightsPerDay", DIVIDE ( Fact_ReservasPickup[RN OTB], DATEDIFF ( Fact_ReservasPickup[FECHAENTRADA], Fact_ReservasPickup[FECHASALIDA], DAY ) + 1 ) // Distribute RN across the days
)
RETURN
SUMX (
RoomNightsPerDay,
[RoomNightsPerDay] // Sum the RN per day for the selected date
)
Sure! I understand your goal now: you want to distribute the total "room nights" (RN OTB) across each day between the CHECK-IN DATE and DEPARTURE DATE for every reservation. The current DAX measure you provided is summing the RN OTB based on the selected date, but it doesn’t account for distributing the room nights over the full stay period.
Here’s how you can adjust your DAX measure to distribute the "room nights" correctly:
If a reservation spans from March 28th to April 4th with 6 room nights (RN OTB = 6), then each day between March 29th and April 4th will be allocated 1 room night.
You can test this measure in a table visual with the selected date on the rows to see how the room nights are distributed across the days. You might want to have a relationship between your date table (A0Dim_Fecha) and your reservation table (Fact_ReservasPickup) for this to work effectively.
This measure should now distribute the room nights properly across each day of the stay period for each reservation. Let me know if you need further adjustments!
¿Respondí a tu pregunta? ¡Marca mi publicación como solución, esto ayudará a otros!
Si mi respuesta(s) te ayudó de alguna manera, no olvides dejarme un "Kudos" 🙂
Saludos cordiales,
Poojara
Analista de Datos | Desarrollador MSBI | Consultor Power BI
YouTube: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS
Hi @Arnauar97 ,
Based on the description, please create the Date Table.
DateTable = CALENDAR(MIN(Fact_ReservasPickup[FECHAENTRADA]), MAX(Fact_ReservasPickup[FECHA_SALIDA]))
Then, creating a measure to filter RN.
RoomNightsPerDay =
VAR CheckInDate = Fact_ReservasPickup[FECHAENTRADA]
VAR CheckOutDate = Fact_ReservasPickup[FECHA_SALIDA]
VAR RoomNights = Fact_ReservasPickup[RN OTB]
RETURN
SUMX(
FILTER(
DateTable,
DateTable[Date] >= CheckInDate && DateTable[Date] < CheckOutDate
),
1
)
Besides, create the measure to sum Room Nights.
TotalRoomNights =
SUMX(
Fact_ReservasPickup,
[RoomNightsPerDay]
)
If the calculated measure is incorrect, please provide some sample data.
Best Rergards,
Wisdom Wu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
in DAX you can use CALENDAR and GENERATE. But it might be simpler to do this in Power Query.
Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).
Do not include sensitive information. Do not include anything that is unrelated to the issue or question.
Need help uploading data? https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Please show the expected outcome based on the sample data you provided.
Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...
User | Count |
---|---|
16 | |
8 | |
7 | |
6 | |
6 |
User | Count |
---|---|
26 | |
13 | |
12 | |
8 | |
8 |