This is a shorter form of my answer at https://tex.stackexchange.com/a/353536/31729, adapted to provide only the number of the parent counter value, not its name as well.
Most of the functionality comes from the zref package, the counter reset list features are determined in my xassoccnt package.
The zref code adds the value of the parent counter.
The first compilation stores the values of the parent counter, but that value is not accessible right from the start of course, that's why \zref@ifrefundefined is applied for checking.
With other words: Compile at least twice. After (or better: during) the first compilation macro \parentref returns -100.
The macro \parentref is expandable.
Make sure to keep the calls
\GetAllResetLists% Important
\RegisterPostLabelHook{\zlabel}% Important
right before \begin{document}!
If any counter is defined afterwards with \newcounter, \GetAllResetLists must be called again to get the correct information about the reset lists up to date.
Here's the very short version:
\documentclass{article}
\usepackage{lipsum}
\usepackage{xassoccnt}
\usepackage[user,counter]{zref}
\usepackage{xparse}
\makeatletter
% Define new properties
\zref@newprop{childcountervalue}{\arabic{\LastRefSteppedCounter}}% This is the naked value
\zref@newprop{parentcountervalue}{\csname the\GetParentCounter{\LastRefSteppedCounter}\endcsname}
% Add the new properties to the main property list stored with \zlabel
\zref@addprops{main}{childcountervalue,parentcountervalue}
\NewDocumentCommand{\parentref}{m}{%
\zref@ifrefundefined{#1}{%
-100%
}{%
\zref@extract{#1}{parentcountervalue}%
}%
}
\makeatother
\GetAllResetLists% Important
\RegisterPostLabelHook{\zlabel}% Important
\begin{document}
\section{My Section}
\lipsum[1]
\subsection{My Sub Section}
\label{subancor}
\lipsum[1]
\section{Foo section}
\subsection{Foobar subsection} \label{foobarsubsection}
Try to get the number of the section not of the subsection: \parentref{subancor} and \parentref{foobarsubsection}
The sum is \the\numexpr\parentref{subancor}+\parentref{foobarsubsection}
\end{document}
