How to show shows values based on one filter at a time selected?

I am filtering data from an excel sheet by date, month and year. When a date is selected from the date filter some percentage values are calculated for that selection, when a month is selected from the month filter then percentage values are calculated for that selected data. The same is for the year filter. I want only the day wise values to be displayed when any date is selected. The other values should not be calculated and month wise values to be displayed when the month is selected and year wise values only when the year is selected. But when I select a date, then values for day wise, month wise and year wise, all are calculated and displayed. I tried using if att_date is not None: , as I only want the values to be selected if the date is selected before the values are getting calculated but it does not work.

Here is my code:

st.sidebar.header("PLease filter here:")

att_date = st.sidebar.multiselect("Select the date:",
                             options = df['Date'].unique())
att_month = st.sidebar.multiselect("Select the month:",
                                   options=df['Month'].unique())
att_year = st.sidebar.multiselect("Select the year:",
                                   options=df['Year'].unique())

df_selection = df.query("Date==@att_date or Month==@att_month or Year==@att_year")

total_count = df_selection.shape[0] 
present_no = (df_selection["Attendance"]=="P").sum()
present_per_cent = (present_no/total_count)*100
sl_no = (df_selection["Attendance"]=="SL").sum()
sl_per_cent = (sl_no/total_count)*100
pl_no = (df_selection["Attendance"]=="PL").sum()
pl_per_cent = (pl_no/total_count)*100
upl_no = (df_selection["Attendance"]=="UPL").sum()
upl_per_cent = (upl_no/total_count)*100

if att_date is not None:
    st.header("Day wise:")
    col_1, col_2, col_3, col_4,col_5 = st.columns(5)
    with col_1:
        st.subheader("Headcount:")
        st.subheader(total_count)
    with col_2:
        st.subheader("SL percentage:")
        st.subheader(sl_per_cent)
    with col_3:
        st.subheader("PL percentage:")
        st.subheader(pl_per_cent)
    with col_4:
        st.subheader("UPL percentage:")
        st.subheader(upl_per_cent)
    with col_5:
        st.subheader("Present percentage:")
        st.subheader(present_per_cent)


st.markdown("---")
# st.header("Week wise:")
if att_month is not None:
    st.header("Month wise:")
    col_6, col_7, col_8, col_9,col_10 = st.columns(5)
    with col_6:
        st.subheader("Headcount:")
        st.subheader(total_count)
    with col_7:
        st.subheader("SL percentage:")
        st.subheader(sl_per_cent)
    with col_8:
        st.subheader("PL percentage:")
        st.subheader(pl_per_cent)
    with col_9:
        st.subheader("UPL percentage:")
        st.subheader(upl_per_cent)
    with col_10:
        st.subheader("Present percentage:")
        st.subheader(present_per_cent)

st.markdown("---")

if att_year is not None:
    st.header("Year wise:")
    col_11, col_12, col_13, col_14,col_15 = st.columns(5)
    with col_11:
        st.subheader("Headcount:")
        st.subheader(total_count)
    with col_12:
        st.subheader("SL percentage:")
        st.subheader(sl_per_cent)
    with col_13:
        st.subheader("PL percentage:")
        st.subheader(pl_per_cent)
    with col_14:
        st.subheader("UPL percentage:")
        st.subheader(upl_per_cent)
    with col_15:
        st.subheader("Present percentage:")
        st.subheader(present_per_cent)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.